Search by foreign key id in admin
Asked Answered
P

3

32

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.

Petropavlovsk answered 7/2, 2012 at 9:48 Comment(0)
S
73

as with the other admin options, you need to use __ for foreign keys e.g.

search_fields = ['id', 'transaction__id']
Syllogize answered 7/2, 2012 at 9:53 Comment(6)
It doesn't seem to work if you're going up two levels of foreign keys, i.e. 'allocation__project__name' doesn't work where allocation has an FK to project and project has a name. How do you solve that? I keep getting the "<field> is not a callable, an attribute of <model>"Fichtean
@radtek, that seems to work for me, I can do fk1__fk2__field, only tried in 1.7 tho, are allocation and project just ForeignKey()?Syllogize
Yeah I know it should work, I even seen an example on django docs but for some reason it isn't working for me. If I don't find a way around it I'll setup a sample repo with the conflicting code.Fichtean
@Fichtean sorry late reply, strange, probably not, but have you tried removing all the other admin options, just to make sure its not misleading message?Syllogize
@Syllogize I'll see if I can isolate it and recreate the issueFichtean
You'll want to use the equals sign with something like search_fields = ('=id',). Otherwise by default an like query will be done, so a query for 6 could also bring back results for 1556. Not usually the intention.Reviviscence
O
10

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']
Orsini answered 7/2, 2012 at 9:52 Comment(1)
For those getting a Related Field got invalid lookup: icontains error, check that you're actually using the related lookup transaction__id instead of the FK column name transaction_id.Neukam
U
0

By default, the Id field which is the primary key in Django is a BigIntergerField right? That's numbers.

So in one of the model's field transaction table, You should set a primary_key=True.

Then when you reference it, you can enter the name of the foreign key instance.

Eg.

Table Category with one field 'name' is a foreign key in post_category:

I set the field 'name' to be its primary key. So to reference it in searchfields,

I will type:

category__name

Doing this allows me to search by category.

You will need to delete your database and re_migrate unless it'd throw a database error because of the previously stored primary key.

If the answer above doesn't work for you, try this. It totally worked for me.

Ushas answered 2/10, 2022 at 12:52 Comment(3)
This does not add much on top of the existing answers, and hardly addresses the actual question.Labroid
Uhmm, I am confused though because I got exactly the same errors. I checked all over the net and couldn't find the solution.Ushas
I figured the answer by myself and thought it'd be nice to help others. How do you know it doesn't address the question. Faced something similar?Ushas

© 2022 - 2024 — McMap. All rights reserved.