1

I have a multiple product models:

class ProductModelA(..

class ProductModelB(..

class ProductModelC(..

Model Order and SubOrder which stores information about quantity. So every Order can have multiple SubOrders which stores tuple product and quantity.

class Order(..

class SubOrder(models.Model):
    order = models.ForeignKey('orders.Order',on_delete=models.CASCADE,related_name='suborders')
    product = GenericForeignKey()
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, related_name='suborders')
    object_id = models.PositiveIntegerField()
    amount = models.PositiveIntegerField(..)

I'm trying to make user friendly admin interface with inline suborder. Choosing from existing products would be enough (alongside with amount field) but can't figure out how to do that.

I tried:

class SubOrderInline(GenericTabularInline):
    model = SubOrder


@admin.register(Order)
class OAdmin(admin.ModelAdmin):
    inlines = [SubOrderInline]

But it doesn't work:

enter image description here

I don't want to choose from Order objects, I want to choose from either ProductModelA,ProductModelB or ProductModelC objects. Is there a built in way?

Milano
  • 18,048
  • 37
  • 153
  • 353
  • Have you tried using normal `TabularInline` instead of the generic one? GenericInline should be used when you have the generic relation between the main model (the one that you're editing) and inline model. – GwynBleidD Feb 28 '19 at 16:21
  • Yes, but it is not userfriendly, you have to choose content type and then raw id. I would rather something like 3 dropdowns. – Milano Feb 28 '19 at 16:22
  • You can provide your own form widget for that. If you want to have something better, look at [models inheritance](https://docs.djangoproject.com/en/2.1/topics/db/models/#model-inheritance) and [Django polymorphic](https://django-polymorphic.readthedocs.io/en/stable/) – GwynBleidD Feb 28 '19 at 16:27
  • Unclear of what you're asking. Seems like using inlines is a bad idea. Perhaps you want to have 3 FKs or an M2M – dan-klasson Feb 28 '19 at 16:39
  • As mentioned above but to make it more clear to you: you cannot use a GenericInline, as the relation between Order and SubOrder is a regular ForeignKey - so use TabularInline. What you are looking for is a way to make a Genericforeignkey Field userfriendly. I have used a module called Django Generic Smart Selects for that in the past but I am not sure how well that is maintained. – ger.s.brett Mar 01 '19 at 06:45

0 Answers0