I'm trying to achieve something apparently simple but I couldn't find any answer, neither on Google nor here. I have a Django model, something dead-simple:
class Shipment(models.Model):
id = models.AutoField(primary_key=True)
transaction = models.ForeignKey(Transaction)
I would like to be able to search in my Shipment Admin page by transaction.id. To clarity, I want this (this code obviously doesn't work):
class ShipmentAdmin(admin.ModelAdmin):
list_display = ('id', 'transaction')
search_fields = ['id', 'transaction.id']
This can't work cause transaction.id does not name a field. Any idea? By "search" I mean be able to insert my transaction id inside the search box of the Shipment Admin page, press "search" and automatically retrieve appropriate transactions.
as with the other admin options, you need to use __ for foreign keys e.g.
search_fields = ['id', 'transaction__id']
search_fields documentation:
You can also perform a related lookup on a ForeignKey or ManyToManyField with the lookup API "follow" notation
Solution:
search_fields = ['id', 'transaction__id']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With