Code

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#5534 closed (invalid)

stand-alone python script: no documentation avaliable

Reported by: anonymous Owned by: nobody
Component: Documentation Version: master
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: UI/UX:

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.

Attachments (0)

Change History (6)

comment:1 Changed 7 years ago by anonymous

  • Cc DXpublica@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 follow-up: Changed 7 years ago by 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 Poll

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.

comment:4 Changed 7 years ago by DXpublica@…

Many thanks for your answers.

Xan.

comment:5 in reply to: ↑ 2 ; follow-up: Changed 7 years ago by 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 Poll

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.

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 in reply to: ↑ 5 Changed 7 years ago by DXpublica@…

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 Poll

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.

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.

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.