开发者

what is the simplest way to create a table use django db api ,and base on 'Standalone Django scripts'

we can call this 'Standalone Django table'

i am not successful now .

can you ???

thanks

if you don't know 'Standalone Django scripts', look this http://www.b-list.org/weblog/2007/sep/22/standalone-django-scripts/


2.this is my code:

from django.core.management import setup_environ
from sphinx_test import settings

setup_environ(settings)

import sys
sys.path.append('D:\zjm_code\sphinx_test')


from django.db import models
from djangosphinx.models import SphinxSearch,SphinxQuery开发者_C百科Set



class File(models.Model):
    name = models.CharField(max_length=200)
    tags = models.CharField(max_length=200) 
    objects = models.Manager()
    search  = SphinxQuerySet(index="test1")
    #class Meta:#<-----------  1
    #    app_label = 'sphinx_test'#<------ 2

and my problem is when i add '#' in front of 1 and 2,it's error is :

Traceback (most recent call last):
  File "D:\zjm_code\sphinx_test\books\models.py", line 17, in <module>
    class File(models.Model):
  File "D:\Python25\Lib\site-packages\django\db\models\base.py", line 52, in __new__
    kwargs = {"app_label": model_module.__name__.split('.')[-2]}
IndexError: list index out of range

when i remove '#' in front of 1 and 2,it print nothing,and don't create table yet.

why ??


The article you linked is a pretty damn good explanation of the simplest way to do it.

Edit: Re-arranged this for clarity.

Starting with a fresh app, create a model, sync the database to create the tables, and then use the setup_environ function from within your standalone script.

Of course this is assuming that myapp is in your PYTHONPATH. If it isn't you must append the path to your app before try to import it:

#!/usr/bin/env python

from django.core.management import setup_environ

# If myapp is not in your PYTHONPATH, append it to sys.path
import sys
sys.path.append('/path/to/myapp/')

# This must be AFTER you update sys.path
from myapp import settings
setup_environ(settings)

from myapp.models import Foo, Bar

# do stuff
foo = Foo.objects.get(id=1)
bar = Bar.objects.filter(foo=foo.baz)

Edit #2: In response to the updated code by the OP. You are trying to create a new model from within the standalone script, which is not the right approach. A standalone script should not be used to create new models or applications, but rather to reference already existing data.

So using your example you would need to create a new app from within a project and then create another script to use as the standalone script. So I'll use the creation of standalone.py as an example.

This is what file structure of D:\zjm_code\sphinx_test should look like:

sphinx_test
|-- __init__.py
|-- manage.py
|-- settings.py
`-- urls.py

So first you would need to create a new app from within this project folder. Let's call it file_test and create it with python manage.py startapp file_test. Now the file tree of D:\zjm_code\sphinx_test should look like this:

sphinx_test
|-- __init__.py
|-- __init__.pyc
|-- file_test
|   |-- __init__.py
|   |-- models.py
|   |-- tests.py
|   `-- views.py
|-- manage.py
|-- settings.py
|-- settings.pyc
`-- urls.py

Now you can create your File model in file_test\models.py:

from django.db import models
from djangosphinx.models import SphinxSearch,SphinxQuerySet

class File(models.Model):
    name = models.CharField(max_length=200)
    tags = models.CharField(max_length=200) 
    objects = models.Manager()
    search  = SphinxQuerySet(index="test1")
    #class Meta:#<-----------  1
    #    app_label = 'sphinx_test'#<------ 2

After you create this model, this is where you would execute python manage.py syncdb to create the model tables in the database you have configured for this app.

Then you can create standalone.py that has all the logic to work with the file_test.models.File model you just created:

#!/path/to/python
from django.core.management import setup_environ
import sys
sys.path.append('D:\zjm_code\sphinx_test')

from sphinx_test import settings
setup_environ(settings)

# NOW you can import from your app
from sphinx_test.file_test.models import File

f = File(name='test', tags='abc,xyz,', search='foo')
f.save()

# confirm the data was saved
if f.id:
    print 'success!'
else:
    print 'fail!'

You have now created a standalone script that can interact with the Django ORM without need for a webserver or a testserver instance running. This is why it is considered to be standalone, because the new script you created may be executed at the command-line alone.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜