'nested' GFK in admin or maybe a custom solution?

16 views
Skip to first unread message

jaddison

unread,
Jun 14, 2017, 5:09:01 PM6/14/17
to Django users
Given the following, where a Product can be associated with multiple Images via a GFK:

# models:

class Image(models.Model):
    image
= ImageField(...)

class ImageAssociation(models.Model):
    image
= models.ForeignKey(Image, related_name='associations')
    content_object
= GenericForeignKey()
    content_type
= models.ForeignKey(ContentType)
    object_id
= models.PositiveIntegerField(db_index=True)

class Product(models.Model):
    title
= models.CharField(max_length=255)
    images
= GenericRelation('images.ImageAssociation', related_query_name='products')

How would I go about adding/changing/deleting Products when editing an Image in the admin? I can easily add/change/delete `ImageAssociation` via admin inlines, but I am looking to take it another level: add/change/delete related `Product` instances.

I've looked at nesting inlines via https://github.com/theatlantic/django-nested-admin, but it looks like the relations are not set up in a way that works - or at least I cannot figure out how to arrange things.

Is it possible to set up a custom inline with a `Product` modelform that automatically creates/removes/updates `ImageAssociation` relational instances as well?
Reply all
Reply to author
Forward
0 new messages