1

In Django, when I have inherited models, how can I do to make promotion/demotion possible in the admin interface ?

Example :

# models.py
class Person(models.Model):
    name=models.CharField()
class Artist(Person):
    medium=models.CharField()
classScientist(models.Model):
    field=models.CharField()


# admin.py
admin.site.register(Person)
admin.site.register(Artist)
admin.site.register(Scientist)

So in the admin, I can create a person, or an artist, or a scientist in one step.

But:

  • once a Person is created, I can't promote that person to a WorkingPerson (which would need to add a row in the WorkingPerson table without creating a new Person).
  • once a WorkingPerson is created, I can't demote that persone to a Person (which would need to delete the row in the WorkingPerson table only).

Thanks a lot !

Olivier

red
  • 201
  • 1
  • 6
  • For the example you've given, do you really need inheritance? You could create a Job model with a one to one field to Person, then create or delete job objects as people start and finish jobs. – Alasdair Sep 20 '15 at 16:13
  • My example was bad, the models could be Person, Artist, Scientist, etc. I update the question – red Oct 08 '15 at 10:10
  • With your method (which I use for now as a workaround), creating an Artist requires two steps : create the person and then the artist. – red Oct 08 '15 at 10:16
  • It's more important to get the models correct, than to minimise the number of steps. It should be possible to add your child models as inlines to the user model, or you could create your own view. – Alasdair Oct 08 '15 at 11:20

1 Answers1

0

I think this can help you to "cast" this.

person_object.__class__ = WorkingPerson

I read this Post some time ago, maybe can help you too.

Community
  • 1
  • 1
Paulo Pessoa
  • 2,509
  • 19
  • 30
  • I was looking to promote/demote in the admin, and not only to cast, but to actually change the record. – red Oct 08 '15 at 10:17