Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 584937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:57:00+00:00 2026-05-13T14:57:00+00:00

we can call this ‘Standalone Django table’ i am not successful now . can

  • 0

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 ??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:57:01+00:00Added an answer on May 13, 2026 at 2:57 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is a follow up to: Why can’t I call a method outside
How can I construct my ajaxSend call, this seems like the place to put
How is it that tools like this one can make an ajax style call
I know I can call the GetVersionEx Win32 API function to retrieve the Windows
How do you set up IIS so that you can call python scripts from
I noticed that you can call Queue.Synchronize to get a thread-safe queue object, but
How exactly using VB6 can I can call any Windows shell command as you
How can I bind arguments to a Python function so that I can call
I need a function that I can call when somthing is done, for example
Is there a public/government web service that I can call to find out what

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.