5

Playing a little around with Polymorphic and additional plugins I'm wondering how I can prevent some of the base class fields from being showed inside form for child admin 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 fileds of base and child class. I tried to add exclude = list() for child class but this didn't work (no change).

frlan
  • 6,950
  • 3
  • 31
  • 72
  • It might be a real bug as by this github comment https://github.com/chrisglass/django_polymorphic/issues/209#issuecomment-216800243 – frlan May 04 '16 at 10:17

1 Answers1

0

Filtering for classes (equivalent to python's 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 feature 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, cherrypicking objects from multiple 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)> ]

Combining Querysets

Querysets could now be regarded as object containers that allow the aggregation of different object types, very similar to python lists - as long as the objects are accessed 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)> ]
Valeriy Solovyov
  • 5,384
  • 3
  • 27
  • 45