Problem with populate script

已查看 56 次
跳至第一个未读帖子

Daniel Grace

未读,
2014年9月29日 13:51:132014/9/29
收件人 django...@googlegroups.com
I am trying to follow the tutorial at http://www.tangowithdjango.com/
I am stuck in section 5.8 where it shows how to create a script to populate the database.
I created a script called "populate.py" as shown (except I added brackets for the Python 3 print statements),
and saved it in the root directory for my project.
But when I run the script I get an error:

(landy) C:\landy\tango>populate.py
Starting Rango population script...
Traceback (most recent call last):
  File "C:\landy\tango\populate.py", line 59, in <module>
    from rango.models import Category, Page
  File "C:\landy\tango\rango\models.py", line 1, in <module>
    from django.db import models
ImportError: No module named 'django.db'

I tried placing the script in C:\landy\tango\tango and C:\landy\tango\rango directories but I get similar errors.

Is it a better approach to make the populate script into a manage command?
Or should I be using fixtures?

Thanks

robert brook

未读,
2014年9月29日 15:05:462014/9/29
收件人 django...@googlegroups.com
This was really not a core function of the application, so I wold not worry about it.

Just add the data to a csv page and import it into sql lite to get you going.


But I believe that you need to tell Python where the data base is located using the settings file

 import os
 os.environ['DJANGO_SETTINGS_MODULE']='settings'

Collin Anderson

未读,
2014年9月29日 16:19:262014/9/29
收件人 django...@googlegroups.com
Is the path to the django source code is on your PYTHONPATH?

If it's in the same directory, try: PYTHONPATH=. populate.py

Daniel Grace

未读,
2014年10月1日 07:30:582014/10/1
收件人 django...@googlegroups.com
I got this working, I had to do two things.
1. add the following line at the top of the script:
import django
2. add the following line after "os.environ.setdefault...":
django.setup()

回复全部
回复作者
转发
0 个新帖子