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,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
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_environfunction from within your standalone script.Of course this is assuming that
myappis in yourPYTHONPATH. If it isn’t you must append the path to your app before try to import it: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.pyas an example.This is what file structure of
D:\zjm_code\sphinx_testshould look like:So first you would need to create a new app from within this project folder. Let’s call it
file_testand create it withpython manage.py startapp file_test. Now the file tree ofD:\zjm_code\sphinx_testshould look like this:Now you can create your
Filemodel infile_test\models.py:After you create this model, this is where you would execute
python manage.py syncdbto create the model tables in the database you have configured for this app.Then you can create
standalone.pythat has all the logic to work with thefile_test.models.Filemodel you just created: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.