I have the following models.py file:
class Product(Model):
...
class ExtraService(Model):
...
class Order(Model):
...
class OrderItem(Model):
order = models.ForeignKey(verbose_name=_('Order Item'), to=Order)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
item_relation = GenericForeignKey('content_type', 'object_id')
quantity = models.PositiveIntegerField(verbose_name=_('Quantity'), default=0)
And I'm working my admin.py this way:
class OrderItemInlineAdmin(GenericTabularInline):
model = OrderItem
min_num = 0
extra = 0
fields = ('item_relation', 'quantity',)
ct_field = 'content_type'
ct_fk_field = 'object_id'
class OrderAdmin(admin.ModelAdmin):
list_display = ('user_form',)
inlines = (OrderItemInlineAdmin,)
admin.site.register(Order, OrderAdmin)
I can't get my admin to show the OrderItem object inline (which can have a key to either a Product or an ExtraService) with my Order object, followed by its quantity field. Instead, it says the item_relation field is unknown:
FieldError at /admin/product/order/13/
Unknown field(s) (item_relation) specified for OrderItem
How do I bypass this?
PS: I've also tried using my own ModelForm but it still doesn't recognize the item_relation field.
PS1: If I don't define a fields variable in OrderItemInlineAdmin, I end up with something like this, which is incorrect because I have existing OrderItem objects and this assumes I don't (no object selected and no quantity?): 