How to exclude fields from a form created using PolymorphicChildModelAdmin

Playing a bit with Polymorphic and additional plugins, I am wondering how I can prevent some base class fields from being displayed inside the form for the admin child interface. Having this adminy.py for my child class:

from django.contrib import admin from .models import * from partsmanagement.models import Part from polymorphic.admin import PolymorphicParentModelAdmin, PolymorphicChildModelAdmin admin.site.register(Book) class BookAdmin(PolymorphicChildModelAdmin): base_model = Part 

and this admin.py for the base model:

 # -*- coding: utf-8 -*- from django.contrib import admin from .models import * from polymorphic.admin import PolymorphicParentModelAdmin, PolymorphicChildModelAdmin from bookcollection.models import Book from bookcollection.admin import BookAdmin admin.site.register(Part) class PartAdmin(PolymorphicParentModelAdmin): base_model = 'Part' child_models = ( (Book, BookAdmin), ) 

Now the form inside admin shows all the entries of the base and child classes. I tried adding exclude = list () for the child class, but this did not work (no change).

+5
source share
1 answer

Filtering for classes (equivalent to python isinstance ()):

 >>> ModelA.objects.instance_of(ModelB) . [ <ModelB: id 2, field1 (CharField), field2 (CharField)>, <ModelC: id 3, field1 (CharField), field2 (CharField), field3 (CharField)> ] 

In general, including or excluding parts of the inheritance tree:

 ModelA.objects.instance_of(ModelB [, ModelC ...]) ModelA.objects.not_instance_of(ModelB [, ModelC ...]) 

You can also use this function in Q objects (with the same result as above):

 >>> ModelA.objects.filter( Q(instance_of=ModelB) ) 

Polymorphic filtering (for fields in derived classes)

For example, ink objects from several derived classes anywhere in the inheritance tree using Q objects (with the syntax: exact model name + three _ + field name):

 >>> ModelA.objects.filter( Q(ModelB___field2 = 'B2') | Q(ModelC___field3 = 'C3') ) . [ <ModelB: id 2, field1 (CharField), field2 (CharField)>, <ModelC: id 3, field1 (CharField), field2 (CharField), field3 (CharField)> ] 

Querysets Association

Querysets can now be viewed as containers of objects that allow you to aggregate various types of objects, very similar to python lists - as long as the objects are accessible through the manager of a common base class:

 >>> Base.objects.instance_of(ModelX) | Base.objects.instance_of(ModelY) . [ <ModelX: id 1, field_x (CharField)>, <ModelY: id 2, field_y (CharField)> ] 
0
source

Source: https://habr.com/ru/post/1247958/


All Articles