Hello,
I have created the following four model classes:
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:
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:
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:
- AirportAdministrativeData model’s officialAirportName field, or,
- 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. But my novice self is still struggling in being able to implement this feature. Help please.