0

我为我的博客设计了以下模型

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 的帖子页面下,

4

3 回答 3

1

为了使用 AuthorInline,您在 Author 模型中定义了一个外键字段

前任:

class Author(models.Model):
    post = models.ForeignKey('Post')

这意味着一篇文章可能有多个作者。

但是在您的情况下,您拥有正确的模型和文件,其中一篇文章只有一位作者,因此您可以删除 AuthorInline。

如果是标签和类别,您使用的是多对多字段,如果您阅读此文档https://docs.djangoproject.com/en/dev/ref/contrib/admin/#working-会很好多对多模型

您必须重写 CategoryInline 和 TagInline;

class TagInline(admin.StackedInline):
    model= Tag.post.through

class CategoryInline(admin.StackedInline):
    model = Category.post.through
于 2015-12-15T10:20:37.047 回答
0

最后我做了,我想要的,主要是让帖子页面中的类别,作者和标签可以选择,所以要做到这一点,我们需要在帖子模型中添加所有字段,这是修改后的模型

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
#
于 2015-12-16T03:34:22.917 回答
0

这不是内联的用途,您不希望它们在这里。

内联用于反向关系:给定作者,编辑他们的详细信息并在同一页面上输入他们的所有书籍。您的外键和多对多字段最好显示为简单的小部件,这是 Django 默认所做的;作者和类别将显示为允许您选择项目的下拉列表,并且标签将显示为多选框。

您也可以选择在作者管理员中将 Book 注册为内联;这取决于你。

于 2015-12-15T09:59:14.270 回答