Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin GenericForeignKey inline

class MyUser(AbstractBaseUser):
    ...
    content_type = models.ForeignKey(ContentType, limit_choices_to={"model__in": ("agentprofile", "clientprofile"))
    object_id = models.PositiveIntegerField()
    profile = generic.GenericForeignKey('content_type', 'object_id')

class AgentProfile(models.Model):
    license_number = models.CharField(max_length=50, blank=True)

class ClientProfile(models.Model):
    address = models.CHarField(max_length=250)

I have 3 models: MyUser(custom user model), ClientProfile, AgentProfile. How can I edit ClientProfile/AgentProfile on MyUser admin page (like inline)?

like image 839
likeon Avatar asked May 21 '13 18:05

likeon


People also ask

What is Genericrelation in Django?

Basically it's a built in app that keeps track of models from the installed apps of your Django application. And one of the use cases of the ContentTypes is to create generic relationships between models.

What is Inlines in Django?

The admin interface is also customizable in many ways. This post is going to focus on one such customization, something called inlines. When two Django models share a foreign key relation, inlines can be used to expose the related model on the parent model page. This can be extremely useful for many applications.


1 Answers

You need to use GenericTabularInline. See the Django docs.

like image 170
Emil Stenström Avatar answered Sep 22 '22 20:09

Emil Stenström