我是Django的首发并且已经遇到了问题:
我创建了这个模型:
from django.db import models
from django.utils import timezone
import datetime
# Create your models here.
class Receta(models.Model):
receta_text = models.CharField(max_length=100)
pub_date = models.DateTimeField('date published')
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
def __unicode__(self): # Python 3: def __str__(self):
return self.receta_text
class Ingredientes(models.Model):
receta = models.ForeignKey(Receta)
ingrediente_text = models.CharField(max_length=100)
calorias = models.FloatField(default=0.0)
hidratos = models.FloatField(default=0.0)
def __unicode__(self): # Python 3: def __str__(self):
return self.ingrediente_text
但是,我有以下错误:任何想法错误可能是什么?
PD:我在django教程
中包含了完整的过程由于
rcastillo135@Michinin:~/django_learn/nutritest$ python manage.py sql recetas
BEGIN;
CREATE TABLE "recetas_receta" (
"id" integer NOT NULL PRIMARY KEY,
"receta_text" varchar(100) NOT NULL,
"pub_date" datetime NOT NULL
)
;
CREATE TABLE "recetas_ingredientes" (
"id" integer NOT NULL PRIMARY KEY,
"receta_id" integer NOT NULL REFERENCES "recetas_receta" ("id"),
"ingrediente_text" varchar(100) NOT NULL,
"calorias" real NOT NULL,
"hidratos" real NOT NULL
)
;
COMMIT;
rcastillo135@Michinin:~/django_learn/nutritest$ python manage.py validate
0 errors found
rcastillo135@Michinin:~/django_learn/nutritest$ python manage.py syncdb
Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
rcastillo135@Michinin:~/django_learn/nutritest$ python manage.py shell
Python 2.7.3 (default, Sep 26 2013, 20:08:41)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from recetas.models import Receta,Ingredientes
>>> p=Receta.objects.get(id=1)
>>> p
<Receta: Pollo>
>>> p.ingredientes_set.all()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 71, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 96, in __iter__
self._fetch_all()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 854, in _fetch_all
self._result_cache = list(self.iterator())
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 220, in iterator
for row in compiler.results_iter():
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 710, in results_iter
for rows in self.execute_sql(MULTI):
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 781, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py", line 69, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 99, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 450, in execute
return Database.Cursor.execute(self, query, params)
OperationalError: no such column: recetas_ingredientes.receta_id
>>>
非常感谢!
答案 0 :(得分:0)
您可能在添加syncdb
ForeignKey之前创建了模型并运行receta
。 syncdb
不会修改已创建的表。由于您只是在试验,您可以删除已有的db表并再次运行syncdb
以从头开始重新创建它们。
您可以使用第三方South项目来管理数据库迁移 - 此功能的一个版本将包含在2014年发布的Django 1.7中。