#5534 closed (invalid)
stand-alone python script: no documentation avaliable
Reported by: | anonymous | Owned by: | nobody |
---|---|---|---|
Component: | Documentation | Version: | dev |
Severity: | Keywords: | python script stand-alone | |
Cc: | DXpublica@… | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
There is no documentation for how to write a bash python script for manage objects.
Typical use if we want to write python script for doing the same as we do in python manage shell (for example for bulk database updates)
For example, we want to run:
from mysite.polls.models import Poll
import datetime
i = 0
while i<4:
a = str(i)
print a
Poll.create(question=a, data=datetime.date.today())
i = i+1
in shell it's tedious to do. So maybe we could do it in bash:
#!/usr/bin/python
from mysite.polls.models import Poll
import datetime
i = 0
while i<4:
a = str(i)
print a
Poll.create(question=a, data=datetime.date.today())
i = i+1
But there is missing imports. In doc there is no mention
Thanks in advance,
Xan.
Change History (6)
comment:1 by , 17 years ago
Cc: | added |
---|
follow-up: 5 comment:2 by , 17 years ago
comment:3 by , 17 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
follow-up: 6 comment:5 by , 17 years ago
Replying to pytechd:
You can do this yourself easily:
import os os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings' from django.conf import settings from myproject.polls.models import PollThis has come up before so it should probably be added somewhere to the documentation. If it's already there, I can't find it.
I tried some code, and the method that _works_ for me is:
First, set DJANGO_SETTINGS_MODULE:
export DJANGO_SETTINGS_MODULE=analitzador.settings
Secondly, write the script:
import os, sys os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' from django.conf import settings from mysite.polls.models import Poll #Anything you want: print Poll.objects.all()
If the script is in the mysite directory, all work fun! (for example, if mysite is in /home/user/mysite) but if script is in somewhere else, it does not run (for example mysite is in /home/user/mysite and the script is in /home/user/mysite/scripts)
How can I solve that?
Thanks,
Xan.
comment:6 by , 17 years ago
Replying to anonymous:
Replying to pytechd:
You can do this yourself easily:
import os os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings' from django.conf import settings from myproject.polls.models import PollThis has come up before so it should probably be added somewhere to the documentation. If it's already there, I can't find it.
I tried some code, and the method that _works_ for me is:
First, set DJANGO_SETTINGS_MODULE:
export DJANGO_SETTINGS_MODULE=analitzador.settingsSecondly, write the script:
import os, sys os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' from django.conf import settings from mysite.polls.models import Poll #Anything you want: print Poll.objects.all()If the script is in the mysite directory, all work fun! (for example, if mysite is in /home/user/mysite) but if script is in somewhere else, it does not run (for example mysite is in /home/user/mysite and the script is in /home/user/mysite/scripts)
How can I solve that?
Thanks,
Xan.
Definitively, the script is:
import os, sys # Add this line if your script is not in your app directory sys.path.append('[super directory of app path]') # If your project is in '/home/user/mysite/polls', you have to put sys.path.append('/home/user/mysite/') os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' from django.conf import settings from mysite.polls.models import Poll #Anything you want: print Poll.objects.all()
I only put this rectification for documentation purposes.
Thanks,
Xan.
You can do this yourself easily:
This has come up before so it should probably be added somewhere to the documentation. If it's already there, I can't find it.