# How to setup search in django-admin changelists in models related through ForeignKeys?

**URL:** <https://forum.djangoproject.com/t/how-to-setup-search-in-django-admin-changelists-in-models-related-through-foreignkeys/14221>\
**Category:** Using the ORM\
**Created:** [June 10, 2022, 1:23pm UTC](https://forum.djangoproject.com/t/how-to-setup-search-in-django-admin-changelists-in-models-related-through-foreignkeys/14221 "2022-06-10T13:23:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ComputerUser-CU](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/computeruser-cu/32/8576_2.png) [@ComputerUser-CU](https://forum.djangoproject.com/u/ComputerUser-CU)\
**Post date:** [June 10, 2022, 1:23pm UTC](https://forum.djangoproject.com/t/how-to-setup-search-in-django-admin-changelists-in-models-related-through-foreignkeys/14221/1 "2022-06-10T13:23:46Z")

</div>

Hello,

I have created the following four model classes:

```auto
class InfrastructureModel(models.Model):
    ENTRY_ACTIVE_YES_NO_CHOICES = (
        (True, 'Yes'),
        (False, 'No'))

    entryActiveYesNo = models.BooleanField(r"Is this database entry still active? (YES/NO)",
                                      null=False,
                                      blank=False,
                                      unique=False,
                                      choices=ENTRY_ACTIVE_YES_NO_CHOICES,
                                      help_text=r"Is this database entry still active? If it's been changed/modified to something else, mark this as False.")

class AirportAdministrativeData(InfrastructureModel):
    officialAirportName=models.CharField(r"Airport's Official Name",
                                         max_length=100,
                                         null=False, 
                                         blank=False, 
                                         unique=True,
                                         help_text=r"Offical name of the Airport")
    def __str__ (self):
        return self.officialAirportName

class AirportAlternateName(InfrastructureModel):
    parentAirport=models.ForeignKey(AirportAdministrativeData,on_delete=models.RESTRICT,limit_choices_to={'entryActiveYesNo': True},verbose_name="Parent Airport",related_name='AirportOfficialName')
    alternateAirportName=models.CharField(r"Airport's Alternate Name",
                                          max_length=100, 
                                          null=False, 
                                          blank=False,
                                          unique=True,
                                          help_text=r"Alternate name of the Airport, if any.")

class AirportLocation(InfrastructureModel):
    parentAirport=models.ForeignKey(AirportAdministrativeData,on_delete=models.RESTRICT,limit_choices_to={'entryActiveYesNo': True},verbose_name="Parent Airport")
    latitude=models.DecimalField(max_digits=9, decimal_places=6)
    longitude=models.DecimalField(max_digits=9, decimal_places=6)
    airportAddress=models.CharField(r"Airport's Address",
                                          max_length=200, 
                                          null=False, 
                                          blank=False,
                                          unique=True,
                                          help_text=r"Airport's Address")
    airportState=models.ForeignKey(State,on_delete=models.RESTRICT,limit_choices_to={'entryActiveYesNo': True},verbose_name="State")

```

And their corresponding Admin classes are as follows:

```auto
class AirportAdministrativeDataAdmin(admin.ModelAdmin):
    fields = ['officialAirportName', 'entryActiveYesNo']
    list_display = ('officialAirportName', 'entryActiveYesNo')
    search_fields = ['officialAirportName']
    search_help_text="Seach here for offical/alternate name of any airport in the Database."
class AirportAlternateNameAdmin(admin.ModelAdmin):
    fields = ['parentAirport', 'alternateAirportName', 'entryActiveYesNo']
    list_display = ('parentAirport', 'alternateAirportName', 'entryActiveYesNo')
    search_fields = ['alternateAirportName']
    search_help_text="Seach here for offical/alternate name of any airport in the Database."
class AirportLocationAdmin(admin.ModelAdmin):
    fields = ['parentAirport', 'latitude', 'longitude', 'airportAddress', 'airportState', 'entryActiveYesNo']
    list_display = ('parentAirport', 'latitude', 'longitude', 'airportAddress', 'airportState', 'entryActiveYesNo')
    #search_fields = ['parentAirport']
    search_help_text="Seach here for offical/alternate name of any airport in the Database."

```

And their corresponding admin site registrations are as follows:

```auto
admin.site.register(AirportAdministrativeData,AirportAdministrativeDataAdmin)
admin.site.register(AirportAlternateName, AirportAlternateNameAdmin)
admin.site.register(AirportLocation, AirportLocationAdmin)

```

I want to be able to search in the change lists of these respective models using either of the following fields:

1. AirportAdministrativeData model’s officialAirportName field, or,
2. AirportAlternateName model’s alternateAirportName field.  
and return the corresponding search entry.

I have read several articles such as `ForeignKey.related_name` and [this stackoverflow q&a](https://stackoverflow.com/questions/11754877/troubleshooting-related-field-has-invalid-lookup-icontains). But my novice self is still struggling in being able to implement this feature. Help please. 😅

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 13, 2022, 5:15pm UTC](https://forum.djangoproject.com/t/how-to-setup-search-in-django-admin-changelists-in-models-related-through-foreignkeys/14221/2 "2022-06-13T17:15:15Z")

</div>

The docs for the search facility, along with a couple of examples are here: [The Django admin site | Django documentation | Django](https://docs.djangoproject.com/en/4.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.search_fields)
