Django admin inlines: single inline for multiple foreign keys

207 views Asked by At

I have a model that stores Conversations as follows:

class Conversation:
    sender_user = models.ForeignKey("test_app.User", on_delete=models.PROTECT,
                                    related_name="conv_starter_user")
    recipient_user = models.ForeignKey("test_app.User", on_delete=models.PROTECT,
                                       related_name="conv_recipient_user")

It references the User model twice.

I would like to be able to go to Django Admin and see a section called 'Conversations' that would list all conversations that the user participates in, both as a starter and as a recipient. Currently, I am creating two separate inlines ConversationRecipientInline and ConversationSenderInline that I add to the User admin. This splits the Conversation view into two which is not ideal.

1

There are 1 answers

0
Alain Bianchini On

If you have multiple foreign keys to the same model that you want to represent as an inline in the admin, you must specify a single foreign key with the fk_name parameter of the InlineModelAdmin subclass. So you can't achieve what you want with InlineModelAdmin and your model design.