I recently filed a bug about this 
<https://code.djangoproject.com/ticket/25278> and was redirected here for 
discussion. While using call_command (which simplifies calling management 
commands), it occurred to me that the API is a little strange. It currently 
is designed to work like this:

    call_command('my_command', *args, **kwargs)

The problem here is that you pass a string into your command, and then 
Django does the magic of converting that into something that can be 
imported, and then uses argparse to parse args and kwargs. I think a better 
API would be:

    from my_project.my_app.management.commands import my_command
    call_command(my_command, *args, **kwargs)

There are three big advantages of this. First, if you ever change the name 
of your command, a good IDE can realize that it needs to update the import 
statements, and that'll happen automatically. This is good and important in 
larger projects where you can't keep all the code in your head.

Second, this allows code completion from your IDE, making it less likely to 
make a typo or a mistake. Another good thing.

Third, this reduces the amount of string to module importing that Django 
does, and less magic is generally good.

In terms of process, I propose we follow the standard deprecation process. 
At first, it should accept either input, and issue a warning. Over time, it 
should only allow modules.

The bug I filed was closed saying that, there wasn't "sufficient 
justification for the additional complexity that would be required." But 
this should only take a few lines of code to check what the argument is 
(string or module), and then to do the conversion.

I'm curious what the group thinks. This seems like a clear improvement to 
the API to me, but perhaps there's something I'm missing. Happy to discuss.

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/9a613577-e0e2-49c8-9a56-ff2a5cb8c94a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to