2π
β
It is a bit hard to do with your setup. If you use a related_name in your OrderItem model such as
order = models.ForeignKey(Order, related_name='items')
You could use it as a reference from the order to items. But again you have a OneToMany relationship so order have many items. You could crate a property in order to get you something like number_of_items such as
@property
def number_of_items(self):
return self.items.count()
and use that in the OrderAdmin such as
class OrderAdmin(admin.ModelAdmin):
list_display = ('price','number_of_items')
It is much easier if you are trying to access Order from OrderItem ModelAdmin because that returns one object so you could do:
class OrderItemAdmin(admin.ModelAdmin):
list_display = ('itemname',order__price')
note the use of double underscore between order and price.
π€Meitham
0π
I write this function to Order model:
def get_items(self):
text = ""
for i in self.oitems.all():
text = text + '<br />' + i.itemname
return text
get_items.allow_tags = True
And I add related_name=βoitemsβ to Order Key in OrderItem. And it works.
π€Nips
Source:stackexchange.com