我已经启动了一个博客应用程序。该模型如下所示:
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=512)
image = models.ImageField(upload_to='/pathto/blogImages')
body = models.TextField()
visible = models.BooleanField()
date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
def __unicode__(self):
return self.title
class Tag(models.Model):
keyword = models.CharField(max_length=256)
posts = models.ManyToManyField(Post)
def __unicode__(self):
return self.keyword
运行 syncdb 后,我创建了一个 admin.py 文件,如下所示:
from blog.models import Post
from blog.models import Tag
from django.contrib import admin
class TagInline(admin.TabularInline):
model = Tag
extra = 3
class PostAdmin(admin.ModelAdmin):
inlines = [TagInline]
admin.site.register(Post, PostAdmin)
当我访问管理区域(http://localhost:8000/admin/blog/post/add/)时,我收到以下错误:
Exception at /admin/blog/post/add/
<class 'blog.models.Tag'> has no ForeignKey to <class 'blog.models.Post'>
Request Method: GET
Request URL: http://localhost:8000/admin/blog/post/add/
Django Version: 1.4.1
Exception Type: Exception
Exception Value:
<class 'blog.models.Tag'> has no ForeignKey to <class 'blog.models.Post'>
Exception Location: /usr/local/lib/python2.7/dist-packages/django/forms/models.py in _get_foreign_key, line 800
Python Executable: /usr/bin/python
Python Version: 2.7.3
当我在 django 中查找多对五关系时,我发现https://docs.djangoproject.com/en/dev/topics/db/examples/many_to_many/。我一直无法找到我缺少的东西。如何避免错误?