我正在尝试在Django中实现多对一关系。
这是我的代码:
models.py
class Entry(models.Model):
title = models.CharField(max_length=100, unique=True)
slug = models.SlugField(max_length=100, unique=True)
content = models.TextField()
date = models.DateTimeField(db_index=True, auto_now_add=True)
category = models.ManyToManyField(Category)
class Meta:
ordering = ['-date']
verbose_name_plural = 'Entradas'
def __unicode__(self):
return '%s' % self.title
@permalink
def get_absolute_url(self):
return ('blog_entries', None, {'slug': self.slug})
class Comment(models.Model):
date = models.DateTimeField(db_index=True, auto_now_add=True)
content = models.TextField()
entry = models.ForeignKey(Entry, related_name='comments')
#add image field
username = models.SlugField(max_length=100, unique=True)
admin.py
from django.contrib import admin
from blog.models import Entry, Comment
class AdminEntry(admin.ModelAdmin):
list_display = ('title', 'date', 'content','slug')
#prepopulated_fields = {"slug": ("titulo",)}
list_filter = ('title', 'date')
ordering = ('-date',)
search_fields = ('title',)
admin.site.register(Entry, AdminEntry)
class AdminComment(admin.ModelAdmin):
list_display= ('username','content')
admin.site.register(Comment, AdminComment)
然后我这样做:
python manage.py syncdb
python manage.py runserver
当我转到管理员的评论部分时,我明白了:
OperationalError at /admin/blog/comment/
no such column: blog_comment.content
Request Method: GET
Request URL: http://localhost:8000/admin/blog/comment/
Django Version: 1.6.1
Exception Type: OperationalError
Exception Value:
no such column: blog_comment.content
Exception Location: /usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py in execute, line 450
Python Executable: /usr/bin/python
Python Version: 2.7.3
Python Path:
['/home/boel/Dropbox/boeldev',
'/usr/local/lib/python2.7/dist-packages/pip-1.1-py2.7.egg',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-linux2',
'/usr/lib/python2.7/lib-tk',
'/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload',
'/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/PIL',
'/usr/lib/python2.7/dist-packages/gst-0.10',
'/usr/lib/python2.7/dist-packages/gtk-2.0',
'/usr/lib/pymodules/python2.7',
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
'/usr/lib/python2.7/dist-packages/ubuntuone-client',
'/usr/lib/python2.7/dist-packages/ubuntuone-control-panel',
'/usr/lib/python2.7/dist-packages/ubuntuone-couch',
'/usr/lib/python2.7/dist-packages/ubuntuone-installer',
'/usr/lib/python2.7/dist-packages/ubuntuone-storage-protocol']
Server time: Thu, 6 Feb 2014 22:39:05 +0000
我做错了什么?
提前致谢!
答案 0 :(得分:1)
syncdb不会在现有表上添加列。放下表并再次运行,或使用South创建迁移。