Django admin InlineModels for manytomany fields - python

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,

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

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.

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
#

Related

Django admin reverse foregin key relationship

I have a number of different models connected to the User model through a foregin key relationship. I would now like to display all the attributes from the objects connected to the User model in the main admin overview.
models.py
class User(AbstractBaseUser):
username = models.CharField(max_length=60)
lastname = models.CharField(max_length=60, blank=True)
class UserProfile(models.Model):
user_id = models.OneToOneField(
User,
on_delete=models.CASCADE,
primary_key=True,
)
address = models.CharField(max_length=60, blank=True)
admin.py
class UserProfileAdmin(admin.ModelAdmin):
list_display = ('user_firstname', 'user_lastname', 'address')
def user_firstname(self, instance):
return instance.user_id.username
def user_lastname(self, instance):
return instance.user_id.lastname
admin.site.register(UserProfile, UserProfileAdmin)
The code above works perfectly well to display attributes from "User" in "Userprofile", but how do I do this the other way around? In my code I currently have 4 different objects connected to the User object so keen to find a way to display all the data there.
use inlines
models.py
class Author(models.Model):
name = models.CharField(max_length=100)
class Book(models.Model):
author = models.ForeignKey(Author, on_delete=models.CASCADE)
title = models.CharField(max_length=100)
admin.py
class BookInline(admin.TabularInline):
model = Book
class AuthorAdmin(admin.ModelAdmin):
inlines = [
BookInline,
]
more info read https://docs.djangoproject.com/en/3.1/ref/contrib/admin/#inlinemodeladmin-objects

Django ManyToMany CreateView Fields In Both Tables

I have two models, there are Book and Author and I add ManyToMany field inside Book model
class Author(models.Model):
name = models.CharField(verbose_name='name', max_length=50)
created_at = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return unicode(self.name)
class Book(models.Model):
title = models.CharField(verbose_name='title', max_length=50)
authors = models.ManyToManyField(Author) # Many to many
created_at = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return unicode(self.title)
If I want to create CreateView from book and access authors, I can just add code like this
class BookCreateView(CreateView):
model = Book
template_name = "books/book_create.html"
fields = ['title', 'authors']
but something that I want to ask is I want to create CreateView from Author model and add field named books inside it. I tried to code like this
class AuthorCreateView(CreateView):
model = Author
template_name = "books/author_create.html"
fields = ['name', 'books']
and it shows an error "Unknown field(s) (books) specified for Author".
help me masters, I'm new in django
Thanks :)
Since Author model don't have a field named books, you cannot add that in AuthorCreateView fields.
What you should do is first create an instance of Author and then add them as author in books instance.
Example.
book_instance.authors.add(author_instance)
Now, it is solved by using Form and FormView, this is my code
this is my forms.py
class AuthorForm(Form):
name = forms.CharField()
books = forms.ModelMultipleChoiceField(Book.objects.all())
this is my views.py
class AuthorCreateView(FormView):
template_name = "books/author_create.html"
form_class = AuthorForm
def form_valid(self, form):
name = form.cleaned_data['name']
books = form.cleaned_data['books']
author = Author(name=name)
author.save()
book_list = Book.objects.filter(pk__in=books)
for book in book_list:
author.book_set.add(book)
return HttpResponse(author.book_set.all())
btw thanks #kartikmaji , you have saved my day :)

Dynamic choices in Foreignkey Field in Django Rest Framework

I have just started learning Django Rest Framework and trying to make a simple API using Django rest Framework.
This is my models.py
from __future__ import unicode_literals
from django.db import models
class Student(models.Model):
created = models.DateTimeField(auto_now_add=True)
name = models.CharField(max_length=150, blank=False)
student_id = models.CharField(max_length=20, primary_key=True)
father_name = models.CharField(max_length=150)
mother_name = models.CharField(max_length=150)
class Meta:
ordering = ('student_id',)
class Subject(models.Model):
created = models.DateTimeField(auto_now_add=True)
subject_id = models.CharField(max_length=20, primary_key=True)
name = models.CharField(max_length=150)
class Meta:
ordering = ('subject_id',)
class Result(models.Model):
created = models.DateTimeField(auto_now_add=True)
grade = models.DecimalField(max_digits=5, decimal_places=3, blank=False)
student_id = models.ForeignKey(Student, on_delete=models.CASCADE)
subject_id = models.ForeignKey(Subject, on_delete=models.CASCADE)
class Meta:
ordering = ('created',)
And this is my serializers.py
from rest_framework import serializers
from models import *
class StudentSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Student
fields = ('student_id', 'name', 'father_name', 'mother_name')
class SubjectSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Subject
fields = ('subject_id', 'name')
class ResultSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Result
fields = ('grade', 'student_id', 'subject_id')
In my "Result" model, I have two foreign keys; student_id and subject_id. This is how it looks like:
My questions is, how can I show the "name" field in the drop down menu in stead of showing "Student Object" and "Subject Object"?
I have tried with
STUDENT_CHOICES = [(each.student_id, each.name) for each in Student.objects.all()]
SUBJECT_CHOICES = [(each.subject_id, each.name) for each in Subject.objects.all()]
in the model's "choices=" field but it didn't work out.
Thanks in advance.
I think you're looking for this part of the DRF documentation.
Basically, your Django model's own representation is used. So for example, in your Student model you can add __str__ method:
# this is for Python 3, use __unicode__ on Python 2
def __str__(self):
return self.name
Meta options documentation for Django is here, look for model methods.

Django forms instead of primary key in django-admin

Suppose I have the following models:
class Product(models.Model):
tags = models.CharField(max_length=50)
created_at = models.DateTimeField(auto_now_add=True)
type = models.ForeignKey(ContentType, related_name='type')
class Flash(models.Model):
name = models.CharField(max_length=50)
class Lense(models.Model):
name = models.CharField(max_length=50)
Is it possible in django admin, instead of having pk to the type field on the Product form, to have all the forms that model with that pk has?
You didn't post ContentType model and you probably didn't set uncode for the model
class ContentType(models.Model):
title = models.CharField(max_length=200)
...
def __unicode__(self):
return self.title
If you do as above you will see Product titles instead of pk

Django why use inline in admin?

Just starting out with Django and I have a basic model for creating blog posts. Basic idea is I want each post to have multiple tags and hopefully later use those for filtering my posts.
class Tag(models.Model):
slug = models.SlugField(max_length=50, unique=True)
def __str__(self):
return self.slug
class Meta:
verbose_name = "Tag"
verbose_name_plural = "Tags"
class Post(models.Model):
post = models.TextField()
pub_date = models.DateTimeField('date published', auto_now=True)
title = models.CharField(max_length=100)
slug = models.SlugField(max_length=100, unique=True)
tags = models.ManyToManyField(Tag)
def __str__(self):
return self.title
class Meta:
verbose_name = "Blog Entry"
verbose_name_plural = "Blog Entries"
ordering = ["-pub_date"]
on my admin side I'm wondering the best way be able to add posts. I've been reading a bunch of threads where people suggest to use an inline style for manytomany relationships, but don't see the difference between that and just editing the object.
So why go with this
class TagInline(admin.TabularInline):
model = Post.tags.through
extra = 1
class newsAdmin(admin.ModelAdmin):
list_display = ('title', 'pub_date')
prepopulated_fields = {"slug": ("title",)}
inlines = [TagInline]
exclude = ('tags',)
admin.site.register(Post, newsAdmin)
admin.site.register(Tag)
over this?
class newsAdmin(admin.ModelAdmin):
list_display = ('title', 'pub_date')
prepopulated_fields = {"slug": ("title",)}
admin.site.register(Post, newsAdmin)
admin.site.register(Tag)
An inline has (almost?) no benefit over a regular field in the case of a default through model for a ManyToManyField. But consider the following (simplified) model:
STATUS = (
('pending', 'Pending'),
('accepted', 'Accepted'),
('rejected', 'Rejected'),
)
class Comment(models.Model):
post = models.ForeignKey(Post)
comment = models.TextField()
status = models.CharField(max_length=20, choices=STATUS)
The primary objective is no longer to connect the right comment to the right post. However, it would be very convenient to moderate all comments on a post on the edit page of the post itself, instead of moderating each comment on a separate page. An inline allows you to change the status of each comment on a single page.
Basically, if you just want to select which items to relate to your primary object, an inline isn't needed. If you need to frequently edit the objects related to your primary object, an inline provides a huge convenience.

Categories