How to syncdb programatically (django standalone script) ?

841 views
Skip to first unread message

Mathieu Leplatre

unread,
Sep 15, 2008, 5:51:59 PM9/15/08
to Django users
Hi all,

I found many post about specific errors regarding django as a
standalone tool.

With a little bit of researching, I ended up with this script below.
Unfortunately, it fails on database initialization.

...
...
File "/home/mathieu/Code/uhm/svn/uhm/django/db/backends/sqlite3/
base.py", line 167, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: polls_poll

The sqlite file is empty (0 byte). How can I syncdb programatically
(and only once) ?

Thanks !


import datetime
from django.conf import settings
settings.configure( DATABASE_ENGINE = "sqlite3",
DATABASE_NAME = "./polls.db",
INSTALLED_APPS = ('polls'))
from django.db import models

class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Meta:
app_label = "polls"

p = Poll(question="What's up?", pub_date=datetime.datetime.now())
p.save()

Steve Holden

unread,
Sep 15, 2008, 9:50:24 PM9/15/08
to django...@googlegroups.com
This is k=nd of a wild-assed guess. Please tell me if it works.

from django.core.management import call_command
call_command('syncdb')

regards
Steve

James Bennett

unread,
Sep 16, 2008, 12:31:13 AM9/16/08
to django...@googlegroups.com
On Mon, Sep 15, 2008 at 8:50 PM, Steve Holden <hold...@gmail.com> wrote:
> This is k=nd of a wild-assed guess. Please tell me if it works.
>
> from django.core.management import call_command
> call_command('syncdb')

That is the way to do it; the whole manage.py/django-admin.py system
was refactored a while back to expose that interface so people can
reach in and call commands programmatically.


--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

Mathieu Leplatre

unread,
Sep 16, 2008, 8:12:18 AM9/16/08
to Django users
Thanks for the tip.


Indeed it loooks to be the way, however I am having app_label
errors...
It complains
Error: No module named p
(for polls)

If I put my script in a module or remove Meta :
File "[...]/django/db/models/base.py", line 51, in __new__
kwargs = {"app_label": model_module.__name__.split('.')[-2]}
IndexError: list index out of range

I wanted a simple standalone script with basic database store /
retrieve of objects. Is it utopian ?
Thanks again

On Sep 16, 1:31 am, "James Bennett" <ubernost...@gmail.com> wrote:

Mathieu Leplatre

unread,
Sep 16, 2008, 8:53:32 PM9/16/08
to Django users
Should I post a new thread ?
I cannot find anything about this way of using Django...

Ronaldo Zacarias Afonso

unread,
Sep 17, 2008, 7:15:20 AM9/17/08
to django...@googlegroups.com
Hi, I think you can use "Django signal feature". Threre is
documentation about that in the Django documentation web site.
I hope it was helpfull ....

[]s
Ronaldo.

Mathieu Leplatre

unread,
Sep 17, 2008, 8:07:39 AM9/17/08
to Django users
Ronaldo, from what I read about signals, I don't think they would help
me writing a standalone script...

On Sep 17, 8:15 am, "Ronaldo Zacarias Afonso"
<ronaldoafo...@gmail.com> wrote:
> Hi, I think you can use "Django signal feature". Threre is
> documentation about that in the Django documentation web site.
> I hope it was helpfull ....
>
> []s
> Ronaldo.
>

Fábio Santos

unread,
Apr 23, 2012, 5:36:58 AM4/23/12
to django...@googlegroups.com
There's a problem in your INSTALLED_APPS setting. You have to use:

('polls',)

Notice the trailing comma. When writing tuples with one item, you must have a traling comma. It indicates that you are writing a tuple, and not simply an expression enclosed in parenthesis. A workaround is to use a list instead, thus ['polls'].

Django will try to iterate INSTALLED_APPS and import all the modules there, in order to do its magic. If you iterate ('polls') you get every character in the string.
Reply all
Reply to author
Forward
0 new messages