2

I have a Person model which has many Animal models as pets. Dog is an Animal with a "favorite bone" field, and Cat is an Animal with a "likes catnip?" field and a "favorite fish" field.

#models
class Person(db.model):
   pass

class Animal(db.model):
   models.ForeignKey(Person) #owner
   name = CharField()

class Dog(Animal):
   favorite_bone = CharField()

class Cat(Animal):
    favorite_fish = CharField()
    likes_catnip = BooleanField()

I would like to inline edit all of a Persons pets, in the Person admin form however, I've read that Django inline admin forms don't support polymorphic inline forms[1], in that, you will only get the parent class fields (e.g. not the favorite_bone or favorite_fish and likes_catnip fields.

Where does this problem come from?

What changes could be made to the framework to accommodate this?

If these changes should not be made, why not?

[1] http://www.mail-archive.com/django-users@googlegroups.com/msg66410.html

mkirk
  • 3,965
  • 1
  • 26
  • 37

3 Answers3

3

(This is an old question, but I thought I'd add an answer in case it is still useful. I've been working on a similar question recently.)

I believe it would be challenging to change Django form-generation to do what you want. The reason is that the inline formset uses a single class/form for all rows of the inline -- there are no configuration options that are evaluated per-row of the inline form. I have convinced myself of this by reading the code itself --- look for "inline" and "formset" in django.contrib.admin.options.py, especially lines 1039-1047 (version 1.5.1). This is also the reason why you can't have some fields read-only in existing items and changeable in new items (see this SO question, for example).

The workarounds found for the readonly case have involved a custom widget that produces the desired behavior such as this one. That still won't directly support polymorphism, however. I think you would need to end up mapping your divergent types back to a common ancestor (e.g. have all pet classes able to return a dict of their unique attributes and values), and then create a single custom widget that renders out the polymorphic part for you. You'd then have to map the values back on save.

This might be more challenging than it is worth, and may lead back to the suggestion in the other answer to not use admin for this :-)

Community
  • 1
  • 1
Denise Draper
  • 1,027
  • 10
  • 24
1

may have a look here.

but i think the modeladmin is currently not able todo such things. you are able to create a custom edit view for your model... there is almost everything possible.

Community
  • 1
  • 1
mo.
  • 3,474
  • 1
  • 23
  • 20
0

It may be possible to do this with Generic Relations.

Dan Lipsitt
  • 175
  • 1
  • 11
  • 1
    generic relations does not fit here.they help to associate models via contenttypes. – mo. Jan 08 '12 at 11:58