0

I have designed following models for my blog

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

and I am trying to register this model under django admin in such a way that. I can edit the Category, Tags and Authors from the Post page. but I am having hard time to accomplish this talk, I have written this code in admin.py file

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',)

When I run my server I got the errors like

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'.

when investigating the error, we cannot have StackedInline class if the models doesn't have foreign key, but How can I put the Tags, Category and Author rendered formm under the Post page in django admin,

shining
  • 1,049
  • 16
  • 31

3 Answers3

1

For using AuthorInline, you ned a foreignkey field in you Author model

ex:

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

This means one post may have multiple authors.

But here in your situation you have the correct model and fileds which have one author for one post, so you can remove AuthorInline.

And incase of Tag and Category, you are using many-to-many field, It will be good if you go through this documentation https://docs.djangoproject.com/en/dev/ref/contrib/admin/#working-with-many-to-many-models

You have to rewrite the CategoryInline and TagInline;

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

class CategoryInline(admin.StackedInline):
    model = Category.post.through
Geo Jacob
  • 5,909
  • 1
  • 36
  • 43
  • But the first logic will change my Business logic, as I want an author can have many post as he can, but two author cannot co-author a post. For the other inline I will check the doc – shining Dec 15 '15 at 10:23
  • Then you have to add a foreignkey feild in Author model and remove author field in Post; or you should change author feild as maytomany instead of foreignkey – Geo Jacob Dec 15 '15 at 10:45
0

This isn't what inlines are for, and you don't want them here.

Inlines are for the reverse relation: given an author, edit their details and enter all their books on the same page. Your foreign keys and many-to-many fields are best shown as simple widgets, which is what Django does by default; the author and category will be displayed as a dropdown allowing you to choose an item, and the tags will be displayed as a multi-select box.

You might also choose to register Book as an inline on the Author admin; that's up to you.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • no in my post page, I am able to see the post_title, post_body, author as a dropdown, both categories and tag are not shown here – shining Dec 15 '15 at 10:06
0

Finally I made, what I wanted, the main gist is to make the category, author and tags choosable from the post page, so to do that, we need to add all the fields in the post model, which is the modified model

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
#
shining
  • 1,049
  • 16
  • 31