Python 如何在django admin中关联同一页面上的多对多关系?
我是django的新手,我正在尝试创建一个简单的博客应用程序 在my models.py中,我为post、COMMETN和标记定义了3个模型 型号.pyPython 如何在django admin中关联同一页面上的多对多关系?,python,django,tags,django-admin,many-to-many,Python,Django,Tags,Django Admin,Many To Many,我是django的新手,我正在尝试创建一个简单的博客应用程序 在my models.py中,我为post、COMMETN和标记定义了3个模型 型号.py from django.db import models from django.contrib.auth.models import User # Create your models here. class Post(models.Model): title = models.CharField(max_length=200
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=200)
body = models.TextField('post body')
author = models.ForeignKey(User)
pub_date = models.DateTimeField('date published')
is_published = models.BooleanField(default=0)
featured_image = models.CharField(max_length=200)
created_date = models.DateTimeField('date created')
updated_date = models.DateTimeField('date Updated')
def __str__(self):
return self.title
class Comment(models.Model):
post = models.ForeignKey(Post)
user = models.ForeignKey(User)
comment_text = models.CharField(max_length=200)
created_date = models.DateTimeField('date created')
is_published = models.BooleanField(default=0)
def __str__(self):
return self.comment_text
class Tags(models.Model):
title = models.CharField(max_length=200)
post = models.ManyToManyField(Post)
def __str__(self):
return self.title
from django.core import serializers
from django import forms
from django.http import HttpResponse
from django.utils import timezone
from django.contrib import admin
from .models import Post, Comment, Tags
# Register your models here.
class CommentsInline(admin.StackedInline):
model = Comment
extra = 1
fields = ['comment_text']
class TagsInline(forms.ModelForm):
# I am not sure what should i put in this class
model = Tags
fields = ('title', )
filter_vertical = ('post', )
class PostAdmin(admin.ModelAdmin):
fieldsets = [
('Content', {'fields': ('title', 'body', 'is_published')}),
('Date Information', {'fields': ('pub_date', )})
]
inlines = [CommentsInline, TagsInline]
admin.site.register(Post, PostAdmin)
正如你所看到的,标签和帖子之间有着多对多的关系
现在,在博客模块的管理面板中,我希望用户能够在同一页面上添加帖子、评论和标签,即(在创建或更新帖子时)
我可以成功地在帖子和评论中这样做,
但是,我不知道如何附加标签,这样我就可以添加新标签,同时将它们附加到帖子上。此外,我想使用标签字段插件
我的管理员py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=200)
body = models.TextField('post body')
author = models.ForeignKey(User)
pub_date = models.DateTimeField('date published')
is_published = models.BooleanField(default=0)
featured_image = models.CharField(max_length=200)
created_date = models.DateTimeField('date created')
updated_date = models.DateTimeField('date Updated')
def __str__(self):
return self.title
class Comment(models.Model):
post = models.ForeignKey(Post)
user = models.ForeignKey(User)
comment_text = models.CharField(max_length=200)
created_date = models.DateTimeField('date created')
is_published = models.BooleanField(default=0)
def __str__(self):
return self.comment_text
class Tags(models.Model):
title = models.CharField(max_length=200)
post = models.ManyToManyField(Post)
def __str__(self):
return self.title
from django.core import serializers
from django import forms
from django.http import HttpResponse
from django.utils import timezone
from django.contrib import admin
from .models import Post, Comment, Tags
# Register your models here.
class CommentsInline(admin.StackedInline):
model = Comment
extra = 1
fields = ['comment_text']
class TagsInline(forms.ModelForm):
# I am not sure what should i put in this class
model = Tags
fields = ('title', )
filter_vertical = ('post', )
class PostAdmin(admin.ModelAdmin):
fieldsets = [
('Content', {'fields': ('title', 'body', 'is_published')}),
('Date Information', {'fields': ('pub_date', )})
]
inlines = [CommentsInline, TagsInline]
admin.site.register(Post, PostAdmin)
在尝试运行上述代码时,我总是看到以下错误:
“blog.Tags”没有指向“blog.Post”的外键
您要查找的是嵌套内联管理。Django没有这个现成的,但是有一个软件包-。几年前,我在Django1.5项目中使用了它,它实现了您似乎想要的功能
对于您的评论,错误消息是正确的。
Tag
和Post
之间没有直接的外键。Django对ManyToManyField
使用中间表。查看并查看是否有帮助。在标签模型中为post字段指定相关名称。尽管Django为反向关系生成了一个默认的相关名称。我猜是“post_set”
然后在管理员模型中进行后期使用
filter_horizontal = ('tags',)
您将在管理面板中看到一个很好的小部件,可以将标签添加到post。它的侧面还有一个+按钮,可以动态添加新标签
不使用TagsInline类
PS:在我看来,你的帖子模型应该包含有很多关系的标签字段,因为事实上,帖子有标签,而不是相反。虽然Django没有区分这两种情况。这对我不适用。使用了软件包,但我仍然面临同样的问题。我已经更新了答案,请检查获取此错误:
filter\u horizontal[0]的值必须是ManytoManyField
您应该在Post模型中创建标记字段。我猜Django不支持对反向ManytoManyField进行水平筛选。加载后模型:tags=models.ManytoManyField(tags,related_name=“posts”)并从标记模型中删除后字段。我得到一个错误,这相当于:配置不当:“PostAdmin.filter\u horizontal”指的是模型“tags”中缺少的字段“tags”
如果我在模型中使用字段名(在上例中为post
),则过滤器\u horizontal工作。