Djangoadminsite (c) inlinemodeladmin

Source: Internet
Author: User


Class Inlinemodeladmin
Class Tabularinline
Class Stackedinline

For example, there are two model:

From django.db import Models

Class Author (models. Model):
Name = models. Charfield (max_length=100)

Class book (Models. Model):
Author = models. ForeignKey (Author)
title = models. Charfield (max_length=100)

If you want to edit book on the author page:

From Django.contrib Import admin

Class Bookinline (admin. Tabularinline):
Model = Book

Class Authoradmin (admin. Modeladmin):
Inlines = [

Django provides a subclass of two inlinemodeladmin:

The difference is in the template used.

Inlinemodeladmin Options

The common options for Inlinemodeladmin and Modeladmin are:

Get_queryset ()
Formfield_for_choice_field ()
Formfield_for_foreignkey ()
Formfield_for_manytomany ()
Has_add_permission ()
Has_change_permission ()
Has_delete_permission ()

The additional options are:

The model used for inline is required.

The name of model, used when there are multiple foreign keys.

Default Baseinlineformset.

Default Modelform. Passed to Inlineformset_factory () when FormSet is created.

The extra number of inline.

Inlinemodeladmin.get_extra () also returns the extra number of inline.

The maximum number that can be displayed.

Inlinemodeladmin.get_max_num () also returns this number.

The minimum number that can be displayed.

Inlinemodeladmin.get_min_num () also returns this number.

With Modeladmin.

Class Bookinline (admin. Tabularinline):
Model = Book
Raw_id_fields = ("pages",)

The template used.

Overwrite the verbose_name in the Meta class.


The default is true.

Inlinemodeladmin.get_formset (Request, Obj=none, **kwargs)
Refer to Modeladmin.get_formsets_with_inlines.

Inlinemodeladmin.get_extra (Request, Obj=none, **kwargs)

Class Binarytreeadmin (admin. Tabularinline):
Model = BinaryTree

def get_extra (self, request, Obj=none, **kwargs):
Extra = 2
If obj:
Return Extra-obj.binarytree_set.count ()
Return extra

Inlinemodeladmin.get_max_num (Request, Obj=none, **kwargs)

Class Binarytreeadmin (admin. Tabularinline):
Model = BinaryTree

def get_max_num (self, request, Obj=none, **kwargs):
Max_num = 10
If obj.parent:
Return max_num-5
Return Max_num

Inlinemodeladmin.get_min_num (Request, Obj=none, **kwargs)
See above.

The case of multiple foreignkey chains to the same model

If there are multiple foreign keys:

From django.db import Models

Class friendship (models. Model):
To_person = models. ForeignKey (person, related_name= "friends")
From_person = models. ForeignKey (person, related_name= "From_friends")

Show one of these:

From Django.contrib Import admin
From Myapp.models Import Friendship

Class Friendshipinline (admin. Tabularinline):
Model = Friendship
Fk_name = "To_person"

Class Personadmin (admin. Modeladmin):
Inlines = [

Working with Many-to-many Models

Model Example:

From django.db import Models

Class person (models. Model):
Name = models. Charfield (max_length=128)

Class Group (models. Model):
Name = models. Charfield (max_length=128)
Members = models. Manytomanyfield (person, related_name= ' groups ')

Inlines Show:

From Django.contrib Import admin

Class Membershipinline (admin. Tabularinline):
Model = Group.members.through

Class Personadmin (admin. Modeladmin):
Inlines = [

Class Groupadmin (admin. Modeladmin):
Inlines = [
Exclude = (' Members ',)


First, the Membershipinline class points to Group.members.through. The through attribute points to the database that manages the many-to-many relationship.

Second, the groupadmin must exclude the Members field.

Working with Many-to-many intermediary models

An example of an intermediate model is clearly indicated:

From django.db import Models

Class person (models. Model):
Name = models. Charfield (max_length=128)

Class Group (models. Model):
Name = models. Charfield (max_length=128)
Members = models. Manytomanyfield (person, through= ' membership ')

Class Membership (models. Model):
Person = models. ForeignKey (person)
Group = models. ForeignKey (Group)
date_joined = models. Datefield ()
Invite_reason = models. Charfield (max_length=64)

The first step:

Class Membershipinline (admin. Tabularinline):
Model = Membership
Extra = 1

Step Two:

Class Personadmin (admin. Modeladmin):
Inlines = (Membershipinline,)

Class Groupadmin (admin. Modeladmin):
Inlines = (Membershipinline,)

Step Three: (person, personadmin) (Group, Groupadmin)

Using generic relations as an inline

An inline with generically related objects example:

From django.db import Models
From Django.contrib.contenttypes.fields import Genericforeignkey

Class Image (models. Model):
Image = Models. ImageField (upload_to= "Images")
Content_Type = models. ForeignKey (ContentType)
OBJECT_ID = models. Positiveintegerfield ()
Content_object = Genericforeignkey ("Content_Type", "object_id")

Class Product (models. Model):
Name = models. Charfield (max_length=100)

If you want to edit an image instance on the Product Add/change page, you can use Generictabularinline or genericstackedinline:

From Django.contrib Import admin
From django.contrib.contenttypes.admin import Generictabularinline

From myproject.myapp.models import Image, Product

Class Imageinline (Generictabularinline):
Model = Image

Class Productadmin (admin. Modeladmin):
Inlines = [
] (Product, Productadmin)

The above is Djangoadminsite (iii) Inlinemodeladmin content, more relevant content please pay attention to (!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.