Custom Django-Admin commands
Hi all, I've written a custom command to use with manage.py, and from the root of the project directory, it works great. But the problem is, this command is almost never going to be called from within the project directory. (hoping the spacing doesn't get too mangled when I send this) /home/ user/ <- ""/home/user/proj/manage.py custom" command doesn't exist. proj/ <- "/home/user/proj/manage.py custom" works manage.py app/ management/ commands/ custom.py Is there something I'm missing? ~Justin -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Custom django-admin commands help?
I have a little custom script that changes some database stuff and sends an email - I want to set it to run on a cron job. So Ive been trying to set it up as a custom django-admin command, Ive been trying to use this little bit of info- http://docs.djangoproject.com/en/dev/howto/custom-management-commands/#howto-custom-management-commands I cannot get it tor work. Ive made the dir struture it suggests and it is finding my python file, Im just not sure how to structure it. I have this in an update.py file- from django.core.management.base import BaseCommand import os import sys class Command(BaseCommand): #--my script-- The error I get is a NotImplementedError. I know it is reading the stuff in place of #--my script-- because it was giving me specific errors related to my script until I fixed them all. I try just putting something simple in place of #--my script-- like x=1 and it still gives me that NotImplementedError. Am I doing this right? What should my update.py file look like? Thanks for any help! Phil --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Custom Django-Admin commands
On Mon, Dec 21, 2009 at 8:10 PM, Justin Steward wrote: > Hi all, > > I've written a custom command to use with manage.py, and from the root > of the project directory, it works great. > > But the problem is, this command is almost never going to be called > from within the project directory. > > (hoping the spacing doesn't get too mangled when I send this) > /home/ > user/ <- ""/home/user/proj/manage.py custom" command doesn't exist. > proj/ <- "/home/user/proj/manage.py custom" works > manage.py > app/ > management/ > commands/ > custom.py > > Is there something I'm missing? You probably just need to set your PYTHONPATH: cd /home/user PYTHONPATH=proj python proj/manage.py custom -Doug > > ~Justin > > -- > > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Custom Django-Admin commands
On Tue, Dec 22, 2009 at 12:16 PM, Doug Blank wrote: > > You probably just need to set your PYTHONPATH: > > cd /home/user > PYTHONPATH=proj python proj/manage.py custom > That was my initial thought too, however setting the PYTHONPATH does not affect the behaviour in this instance. ~Justin -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Custom Django-Admin commands
On Dec 21, 8:38 pm, Justin Steward wrote: > On Tue, Dec 22, 2009 at 12:16 PM, Doug Blank wrote: > > > You probably just need to set your PYTHONPATH: > > > cd /home/user > > PYTHONPATH=proj python proj/manage.py custom > > That was my initial thought too, however setting the PYTHONPATH does > not affect the behaviour in this instance. > > ~Justin Do you have __init__.py files in each of those directories? -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Custom Django-Admin commands
On Tue, Dec 22, 2009 at 3:51 PM, Alex_Gaynor wrote: > > Do you have __init__.py files in each of those directories? Of course - Else it woud not work from the project's root directory either. ~Justin -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Custom Django-Admin commands
I'm at a loss as to understand why, but I've worked out what I need to add to pythonpath. PYTHONPATH=/home/user i.e. The pythonpath needs to include NOT the project's root, but the directory one level ABOVE that for custom commands to work properly... Thanks for the help guys. ~Justin -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Custom Django-Admin commands
If you are calling this from, for example, crontab note that each line in crontab is executed in its own subshell, so you have the choice of setting PYTHONPATH, or (my personal favorite because it works with so many kinds of scripts) you can cd to the project directory and execute the script as ./manage.py custom, by separating the cd and ./manage.py with a semicolon. Too, if you are doing this from a bash (or sh or, probably, csh) script, commands run inside parentheses run in a sub shell, so you can do the cd, semi, manage trick, and when the subshell exits, the rest of your script is still running in its original directory. Finally, if this is a command line utility that you want to run from wherever and just have on the path, create a sh (or bash) script that cd's and runs it instead, and put that on the path. This last works on windows too, using bat files (or whatever the cmd.exe equivalent is). On Tue, Dec 22, 2009 at 5:59 AM, Justin Steward wrote: > I'm at a loss as to understand why, but I've worked out what I need to > add to pythonpath. > > PYTHONPATH=/home/user > > i.e. The pythonpath needs to include NOT the project's root, but the > directory one level ABOVE that for custom commands to work properly... > > Thanks for the help guys. > > ~Justin > > -- > > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Custom Django-Admin commands
On Tue, Dec 22, 2009 at 3:24 PM, Bill Freeman wrote: > If you are calling this from, for example, crontab note that each line > in crontab is executed in its own subshell, so you have the choice of > setting PYTHONPATH, or (my personal favorite because it works with so > many kinds of scripts) you can cd to the project directory and execute > the script as ./manage.py custom, by separating the cd and ./manage.py > with a semicolon. Too, if you are doing this from a bash (or sh or, > probably, csh) script, commands run inside parentheses run in a sub > shell, so you can do the cd, semi, manage trick, and when the subshell > exits, the rest of your script is still running in its original > directory. Finally, if this is a command line utility that you want > to run from wherever and just have on the path, create a sh (or bash) > script that cd's and runs it instead, and put that on the path. This > last works on windows too, using bat files (or whatever the cmd.exe > equivalent is). > Thanks Bill, I'd considered that route, but ultimately, I had a puzzle that needed solving. =) ~Justin -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Writing custom django-admin commands
how should i write commands that works with all apps existing in project not just apps with specific name -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5760e637-570e-4d96-838d-55c4a2ee4d8an%40googlegroups.com.
Re: Custom django-admin commands help?
On Feb 22, 1:00 pm, "phyl.jack...@gmail.com" wrote: > I have a little custom script that changes some database stuff and > sends an email - I want to set it to run on a cron job. > > So Ive been trying to set it up as a custom django-admin command, Ive > been trying to use this little bit of info- > > http://docs.djangoproject.com/en/dev/howto/custom-management-commands... > > I cannot get it tor work. Ive made the dir struture it suggests and it > is finding my python file, Im just not sure how to structure it. I > have this in an update.py file- > > from django.core.management.base import BaseCommand > import os > import sys > > class Command(BaseCommand): > #--my script-- > > The error I get is a NotImplementedError. I know it is reading the > stuff in place of #--my script-- because it was giving me specific > errors related to my script until I fixed them all. I try just putting > something simple in place of #--my script-- like x=1 and it still > gives me that NotImplementedError. Am I doing this right? What should > my update.py file look like? > > Thanks for any help! > Phil You've cunningly cut out from the code you posted the actual contents of the Command class, which is the bit that would enable us to diagnose the problem. But at a guess, you have just put the script directly under the class. It actually needs to go into a method called 'handle', which is what is called when the command is run. Without that method, Django is calling the method in the base class, which raises NotImplementedError to tell you you need to override it. class Command(BaseCommand): help = """Removes doubled up HTML classes caused by a TinyMCE problem""" def handle(self, *args, **options): ... script goes here... -- DR. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Custom django-admin commands help?
Thanks Daniel, your guess was correct Id left out the handle stuff competely. It appears to be working perfectly now! Phil On 22 Feb, 13:21, Daniel Roseman wrote: > On Feb 22, 1:00 pm, "phyl.jack...@gmail.com" > wrote: > > > > > I have a little custom script that changes some database stuff and > > sends an email - I want to set it to run on a cron job. > > > So Ive been trying to set it up as a custom django-admin command, Ive > > been trying to use this little bit of info- > > >http://docs.djangoproject.com/en/dev/howto/custom-management-commands... > > > I cannot get it tor work. Ive made the dir struture it suggests and it > > is finding my python file, Im just not sure how to structure it. I > > have this in an update.py file- > > > from django.core.management.base import BaseCommand > > import os > > import sys > > > class Command(BaseCommand): > > #--my script-- > > > The error I get is a NotImplementedError. I know it is reading the > > stuff in place of #--my script-- because it was giving me specific > > errors related to my script until I fixed them all. I try just putting > > something simple in place of #--my script-- like x=1 and it still > > gives me that NotImplementedError. Am I doing this right? What should > > my update.py file look like? > > > Thanks for any help! > > Phil > > You've cunningly cut out from the code you posted the actual contents > of the Command class, which is the bit that would enable us to > diagnose the problem. But at a guess, you have just put the script > directly under the class. It actually needs to go into a method called > 'handle', which is what is called when the command is run. Without > that method, Django is calling the method in the base class, which > raises NotImplementedError to tell you you need to override it. > > class Command(BaseCommand): > help = """Removes doubled up HTML classes caused by a TinyMCE > problem""" > > def handle(self, *args, **options): > ... script goes here... > > -- > DR. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
custom django-admin commands naming convention
Hi, I need to cron a few jobs for my django app to run daily. I went through the docs on "Writing custom django-admin commands" and thats the direction I'm planning to go to run these jobs. Our company has a number of teams developing different apps and I didn't want the naming of the commands to be a mess. Are there any recommendations for naming conventions of the commands. Is it common for example to call the command appname_command to make it clear which app is implementing which command. Any advice/experience is welcome. thanks, Marwan -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: custom django-admin commands naming convention
Hello Marwan, I don't know if it's common but for my needs it's a must! :-) I have several apps with custom management commands and I've taken to naming the commands like... my_app_name_my_custom_management_command_name.py If find it easier to read the command name if I separate each word with an underscore. The names are fairly long but I'm not typing them into the terminal all that much. Mostly called from cron or pulled up through my bash history. Toodle-looo.. creecode On Mar 8, 12:02 am, Marwan Al-Sabbagh wrote: > Our company has a number of teams > developing different apps and I didn't want the naming of the commands to be > a mess. Are there any recommendations for naming conventions of the > commands. Is it common for example to call the command appname_command to > make it clear which app is implementing which command. Any advice/experience > is welcome. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: custom django-admin commands naming convention
cool thanks, Marwan On Wed, Mar 9, 2011 at 12:14 AM, creecode wrote: > Hello Marwan, > > I don't know if it's common but for my needs it's a must! :-) I have > several apps with custom management commands and I've taken to naming > the commands like... > > my_app_name_my_custom_management_command_name.py > > If find it easier to read the command name if I separate each word > with an underscore. The names are fairly long but I'm not typing them > into the terminal all that much. Mostly called from cron or pulled up > through my bash history. > > Toodle-looo.. > creecode > > On Mar 8, 12:02 am, Marwan Al-Sabbagh > wrote: > > > Our company has a number of teams > > developing different apps and I didn't want the naming of the commands to > be > > a mess. Are there any recommendations for naming conventions of the > > commands. Is it common for example to call the command appname_command to > > make it clear which app is implementing which command. Any > advice/experience > > is welcome. > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Signals sent while running custom django-admin commands?
With Django 1.2.1 I'm finding that signals don't seem to get sent while running a custom manage.py command. Looking at the documentation[1], it looks like this may be the intended behavior; is the documentation saying that _only_ the post_syncdb signal gets sent when running django-admin commands, or that it's the only _additional_ signal that gets sent? (Specifically I have some things that normally happen on post_save that aren't ever getting called.) If it's the only signal that gets sent, I'm curious: why? (And is there any chance additional signals will be supported in the future? Say, all the normal Django ones?) (If this is the intended behavior, it also might be nice to update the docs to specifically state that post_syncdb is the only signal that gets sent from django_admin.) [1] http://docs.djangoproject.com/en/dev/ref/signals/#management-signals -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Followed 'Writing custom django-admin commands' - 'Apps aren't loaded yet.'!
I've got a basic Django site up and running and am now trying to write a custom command by following the "Writing custom django-admin commands" documentation. However having followed it carefully I get this exception: (myfrontier) C:\Users\PDS\git\myfrontier>set DJANGO_SETTINGS_MODULE=myfrontier.settings (myfrontier) C:\Users\PDS\git\myfrontier>django-admin podcasts Traceback (most recent call last): File "C:\virtualenv\myfrontier\Scripts\django-admin-script.py", line 11, in load_entry_point('Django', 'console_scripts', 'django-admin')() File "C:\virtualenv\myfrontier\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line utility.execute() File "C:\virtualenv\myfrontier\lib\site-packages\django\core\management\__init__.py", line 357, in execute django.setup() File "C:\virtualenv\myfrontier\lib\site-packages\django\__init__.py", line 24, in setup apps.populate(settings.INSTALLED_APPS) File "C:\virtualenv\myfrontier\lib\site-packages\django\apps\registry.py", line 91, in populate app_config = AppConfig.create(entry) File "C:\virtualenv\myfrontier\lib\site-packages\django\apps\config.py", line 90, in create module = import_module(entry) File "C:\virtualenv\myfrontier\lib\importlib\__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 994, in _gcd_import File "", line 971, in _find_and_load File "", line 955, in _find_and_load_unlocked File "", line 665, in _load_unlocked File "", line 678, in exec_module File "", line 219, in _call_with_frames_removed File "c:\users\pds\git\myfrontier\portal\management\podcasts.py", line 2, in from portal.models import ShowEpisode File "c:\users\pds\git\myfrontier\portal\models.py", line 4, in class Dir(models.Model): File "C:\virtualenv\myfrontier\lib\site-packages\django\db\models\base.py", line 103, in __new__ app_config = apps.get_containing_app_config(module) File "C:\virtualenv\myfrontier\lib\site-packages\django\apps\registry.py", line 252, in get_containing_app_config self.check_apps_ready() File "C:\virtualenv\myfrontier\lib\site-packages\django\apps\registry.py", line 135, in check_apps_ready raise AppRegistryNotReady("Apps aren't loaded yet.") django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. The first two lines of my custom command are just like the example in the doc... from django.core.management.base import BaseCommand, CommandError from portal.models import ShowEpisode So what am I doing wrong? Is the documentation just wrong? A quick Google throws up lots of suggestions and a number of people hitting this so can someone from Django dev. give some proper chapter and verse on how to solve this please? thanks, Paul DS -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f10ff11c-e954-449b-a156-59953fc836da%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Followed 'Writing custom django-admin commands' - 'Apps aren't loaded yet.'!
Try following this tutorial: https://simpleisbetterthancomplex.com/tutorial/2018/08/27/how-to-create-custom-django-management-commands.html It looks like you don't have apps registered in INSTALLED APPS. Also, I always use the 'python manage.py ' not the 'django-admin ' On Thursday, July 11, 2019 at 3:15:39 PM UTC-5, paul_d...@hotmail.com wrote: > > I've got a basic Django site up and running and am now trying to write a > custom command by following the "Writing custom django-admin commands" > documentation. However having followed it carefully I get this exception: > > (myfrontier) C:\Users\PDS\git\myfrontier>set > DJANGO_SETTINGS_MODULE=myfrontier.settings > > (myfrontier) C:\Users\PDS\git\myfrontier>django-admin podcasts > Traceback (most recent call last): > File "C:\virtualenv\myfrontier\Scripts\django-admin-script.py", line 11, > in > load_entry_point('Django', 'console_scripts', 'django-admin')() > File > "C:\virtualenv\myfrontier\lib\site-packages\django\core\management\__init__.py", > > line 381, in execute_from_command_line > utility.execute() > File > "C:\virtualenv\myfrontier\lib\site-packages\django\core\management\__init__.py", > > line 357, in execute > django.setup() > File "C:\virtualenv\myfrontier\lib\site-packages\django\__init__.py", > line 24, in setup > apps.populate(settings.INSTALLED_APPS) > File > "C:\virtualenv\myfrontier\lib\site-packages\django\apps\registry.py", line > 91, in populate > app_config = AppConfig.create(entry) > File "C:\virtualenv\myfrontier\lib\site-packages\django\apps\config.py", > line 90, in create > module = import_module(entry) > File "C:\virtualenv\myfrontier\lib\importlib\__init__.py", line 126, in > import_module > return _bootstrap._gcd_import(name[level:], package, level) > File "", line 994, in _gcd_import > File "", line 971, in _find_and_load > File "", line 955, in > _find_and_load_unlocked > File "", line 665, in _load_unlocked > File "", line 678, in exec_module > File "", line 219, in > _call_with_frames_removed > File "c:\users\pds\git\myfrontier\portal\management\podcasts.py", line > 2, in > from portal.models import ShowEpisode > File "c:\users\pds\git\myfrontier\portal\models.py", line 4, in > class Dir(models.Model): > File > "C:\virtualenv\myfrontier\lib\site-packages\django\db\models\base.py", line > 103, in __new__ > app_config = apps.get_containing_app_config(module) > File > "C:\virtualenv\myfrontier\lib\site-packages\django\apps\registry.py", line > 252, in get_containing_app_config > self.check_apps_ready() > File > "C:\virtualenv\myfrontier\lib\site-packages\django\apps\registry.py", line > 135, in check_apps_ready > raise AppRegistryNotReady("Apps aren't loaded yet.") > django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. > > The first two lines of my custom command are just like the example in the > doc... > from django.core.management.base import BaseCommand, CommandError > from portal.models import ShowEpisode > > So what am I doing wrong? Is the documentation just wrong? A quick > Google throws up lots of suggestions and a number of people hitting this so > can someone from Django dev. give some proper chapter and verse on how to > solve this please? > > thanks, > Paul DS > -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/7ae0fca5-1f57-457b-a90b-6ca4e2b7b668%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.