我为我的博客设计了以下模型
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=200)
body = models.TextField(default='')
created_at = models.DateTimeField('created date', auto_now_add=True, auto_now=False)
updated_at = models.DateTimeField('updated date', auto_now_add=False, auto_now=True)
author = models.ForeignKey('Author', default='admin')
def __str__(self):
return self.title
class Author(models.Model):
name = models.CharField(max_length=150)
email = models.EmailField(blank=True)
bio = models.TextField()
def __str__(self):
return self.name
class Category(models.Model):
cat_name = models.CharField(max_length=200)
post = models.ManyToManyField('Post')
def __str__(self):
return self.cat_name
class Tag(models.Model):
tag_name = models.CharField(max_length=200)
post = models.ManyToManyField('Post')
def __str__(self):
return self.tag_name
我试图以这样的方式在django admin下注册这个模型。我可以从帖子页面编辑类别,标签和作者。但是我很难完成这个演讲,我已经在admin.py文件中编写了这段代码
from django.contrib import admin
from .models import Post, Author, Tag, Category
class AuthorInline(admin.TabularInline):
model= Author
class TagInline(admin.StackedInline):
model= Tag
class CategoryInline(admin.StackedInline):
model = Category
@admin.register(Post) #another method of registration admin.site.register(Post, PostAdmin)
class PostAdmin(admin.ModelAdmin):
#Show the following fields in this order
fields = ['body', 'title']
#show the following filelds for nice formattng
list_display = ['title', 'author', 'created_at']
#display based on the date hirerarchy
date_hierachy = 'created_at'
#embed the following child models in this parent models
inlines = [AuthorInline, TagInline, CategoryInline,]
#to exclude fields
exclude = ('author',)
当我运行我的服务器时,我遇到了像
这样的错误ERRORS:
<class 'blogs.admin.AuthorInline'>: (admin.E202) 'blogs.Author' has no ForeignKey to 'blogs.Post'.
<class 'blogs.admin.CategoryInline'>: (admin.E202) 'blogs.Category' has no ForeignKey to 'blogs.Post'.
<class 'blogs.admin.TagInline'>: (admin.E202) 'blogs.Tag' has no ForeignKey to 'blogs.Post'.
在调查错误时,如果模型没有外键,我们就不能拥有StackedInline类,但是如何将标签,类别和作者渲染的形式放在django admin的Post页面下,
答案 0 :(得分:1)
对于使用AuthorInline,您在作者模型
中找到了一个外键字段例如:
class Author(models.Model):
post = models.ForeignKey('Post')
这意味着一篇文章可能有多位作者。
但是在你的情况下你有正确的模型和文件,其中一个帖子有一个作者,所以你可以删除AuthorInline。
对于标签和类别,您使用的是多对多字段,如果您查看此文档将会很好https://docs.djangoproject.com/en/dev/ref/contrib/admin/#working-with-many-to-many-models
你必须重写CategoryInline和TagInline;
class TagInline(admin.StackedInline):
model= Tag.post.through
class CategoryInline(admin.StackedInline):
model = Category.post.through
答案 1 :(得分:0)
这不是内联的内容,你不需要它们。
内联是针对反向关系的:给定作者,编辑他们的详细信息并在同一页面上输入他们的所有书籍。您的外键和多对多字段最好显示为简单的小部件,这是Django默认执行的操作;作者和类别将显示为下拉列表,允许您选择项目,标签将显示为多选框。
您可能也选择在作者admin上注册Book作为内联;这取决于你。
答案 2 :(得分:0)
最后我做了,我想要的,主要的要点是从帖子页面选择类别,作者和标签,所以要做到这一点,我们需要在post模型中添加所有字段,这是修改过的模特
from django.db import models
from django.utils import timezone
class Author(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
email = models.EmailField(blank=True)
bio = models.TextField()
class Tag(models.Model):
tag_name = models.CharField(max_length=50)
class Category(models.Model):
cat_name = models.CharField(max_length=50)
class Post(models.Model):
'''post can have many categories
and categories can have many post
author can have many post but post
can have single author
post can have many tags, and tags
can have many posts'''
title = models.CharField('post title', max_length=200)
body = models.TextField(default='', null=True)
created_at = models.DateTimeField(auto_now_add=True, auto_now=False)
updated_at = models.DateTimeField(auto_now_add=False, auto_now=True)
author = models.ForeignKey(Author, verbose_name = "List of Author") #many to one relationship
def __str__(self):
return self.title
#Generally many to many fields should into that model which is going to be edited.
tags = models.ManyToManyField(Tag)
categories = models.ManyToManyField(Category)
class Meta:
ordering = ['-created_at']
verbose_name_plural = "Posteeees"
# def post_status(self):
# return timezone.now() - self.updated_at <= 1
#Recursive realation, we can define the foreignkey itself to the model and this is called rrecursive realation
#