Django 没有foreignkey...但它是ManyToManyField
除了一个大障碍之外,我的管理员工作正常。我在帖子和标签之间创建了多对多关系。我可以在我的管理员中 CRUD 标签,但由于某种原因,我收到以下错误消息:
Exception at /admin/website/post/add/
<class 'website.models.Tag'> has no ForeignKey to <class 'website.models.Post'
这是我的模型:
class Post(models.Model):
user=models.ForeignKey(User, unique=True)
title=models.CharField(max_length=80)
slug=models.SlugField()
description=models.TextField(max_length=1000, blank=True)
created=models.DateField(auto_now_add=True)
#following info is for processing purposes
management_phone=models.CharField(max_length=200, blank=True)
management_email=models.CharField(max_length=200, blank=True)
processing=models.BooleanField(default=False)
transacted=models.BooleanField(default=False)
manually_closed=models.BooleanField(default=False)
def __unicode__(self):
return self.title
class Tag(models.Model):
title=models.CharField(max_length=100)
posts=models.ManyToManyField(Post, blank=True,null=True)
def __unicode__(self):
Return self.title
同样,只有当我尝试添加 Post 实例时才会出现问题
我的数据库中有一个数据库表“website_tag_posts”的关系。这里有什么问题?
谢谢
My admin is working fine other than one big roadbump. I've created a manytomany relationship between posts and tags. I can CRUD tags in my admin but for some reason, I get the following error message:
Exception at /admin/website/post/add/
<class 'website.models.Tag'> has no ForeignKey to <class 'website.models.Post'
Here are my models:
class Post(models.Model):
user=models.ForeignKey(User, unique=True)
title=models.CharField(max_length=80)
slug=models.SlugField()
description=models.TextField(max_length=1000, blank=True)
created=models.DateField(auto_now_add=True)
#following info is for processing purposes
management_phone=models.CharField(max_length=200, blank=True)
management_email=models.CharField(max_length=200, blank=True)
processing=models.BooleanField(default=False)
transacted=models.BooleanField(default=False)
manually_closed=models.BooleanField(default=False)
def __unicode__(self):
return self.title
class Tag(models.Model):
title=models.CharField(max_length=100)
posts=models.ManyToManyField(Post, blank=True,null=True)
def __unicode__(self):
Return self.title
Again, the problem only emerges when I try to Add a Post instance
I have a database table "website_tag_posts" in my database for the relationship. What's the problem here?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,您使用
Tag Inline
设置了Post
ModelAdmin
。ManyToMany 不是外键,因此您不能像这样设置 Inline。
如果您想要内联,请指定 m2m 字段的 through 模型作为模型。 m2m 字段的 through 表包含
Post
表的ForeignKey
。表格标签<-表格tag_post->表帖
Sounds to me like you set up your
Post
ModelAdmin
with aTag Inline
.ManyToMany is not a foreign key, so you can't set up an Inline like that.
If you want an inline, specify the through model for your m2m field as the model instead. The through table for the m2m field contains the
ForeignKey
to yourPost
table.table tag <- table tag_post -> table post