Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show related objects in Django/Admin?

Tags:

I have 2 models:

from django.db import models  class Category(models.Model):     icon = models.ImageField(upload_to = 'thing/icon/')     image = models.ImageField(upload_to = 'thing/image/')     created_at = models.DateTimeField(auto_now_add=True)     updated_at = models.DateTimeField(auto_now=True)     title = models.CharField(max_length=200)     slug = models.SlugField()     desc = models.TextField(max_length=1000)      def __str__(self):         return self.title      def __unicode__(self):         return self.title  class Thing(models.Model):     icon = models.ImageField(upload_to = 'thing/icon/')     image = models.ImageField(upload_to = 'thing/image/')     created_at = models.DateTimeField(auto_now_add=True)     updated_at = models.DateTimeField(auto_now=True)     title = models.CharField(max_length=200)     slug = models.SlugField()     desc = models.TextField(max_length=1000)     content = models.TextField()     category = models.ForeignKey('Category')      def __str__(self):         return self.title      def __unicode__(self):         return self.title 

I am using Django's admin site for basic CRUD operations. I need to show all Things in Category if I am selecting an Category in admin.

like image 206
KenanBek Avatar asked Feb 09 '14 09:02

KenanBek


People also ask

What is Inlines in Django admin?

The admin interface is also customizable in many ways. This post is going to focus on one such customization, something called inlines. When two Django models share a foreign key relation, inlines can be used to expose the related model on the parent model page. This can be extremely useful for many applications.

How use Django admin panel?

To login to the site, open the /admin URL (e.g. http://127.0.0.1:8000/admin ) and enter your new superuser userid and password credentials (you'll be redirected to the login page, and then back to the /admin URL after you've entered your details).


2 Answers

You can use "Inlines" to visualize and edit Things of a certain Category in the admin detail for that category:

In the admin.py file, create an Inline object for Thing (ThingInline) and modify your CategoryAdmin class to have an inline of type ThingInline like this:

... class ThingInline(admin.TabularInline):     model = Thing  class CategoryAdmin(admin.ModelAdmin):     inlines = [         ThingInline,     ] ... 

For further details, this is the docs for admin inlines: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#inlinemodeladmin-objects

like image 137
Matteo Scotuzzi Avatar answered Oct 01 '22 23:10

Matteo Scotuzzi


update admin.py file like this.

from django.contrib import admin from .views import Category, Thing  class CategoryAdmin(admin.ModelAdmin):     inlines = [         ThingInline,     ]  class ThingInline(admin.TabularInline):     model = Thing  admin.site.register(Category, CategoryAdmin)  admin.site.register(Thing) 

There are two inline options TabularInline and StackedInline

like image 42
Shivam Agrawal Avatar answered Oct 01 '22 22:10

Shivam Agrawal