如何在django-admin变更列表中设置搜索通过ForeignKeys相关的模型?



我已经创建了以下四个模型类:

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")

和它们对应的Admin类如下:

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."

对应的管理站点注册如下:

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

我希望能够使用以下字段之一在这些各自模型的变更列表中进行搜索:

  1. AirportAdministrativeData模型的officialAirportName字段,或者
  2. AirportAlternateName模型的alternateAirportName字段

并返回相应的搜索项。

我读了几篇文章,比如django文档中的ForeignKey。Related_name和这个stackoverflow q&a。但是作为新手的我仍然在努力实现这个功能。请帮助!

要从相关模型中添加搜索字段,您需要使用与filter()相同的符号,即用双下划线分隔字段名称的路径。

第一个搜索字段

要将AirportAdministrativeData.officialAirportName作为搜索字段添加到AirportLocation的更改列表中,您需要添加以下内容:'parentAirport__officialAirportName.

第二个搜索字段

对于第二个搜索字段的替代名称,您需要更深入一层并使用"相关名称",这是从AirportAdministrativeDataAdminAirportAlternateNameAdmin的反向关系:

'parentAirport__AirportOfficialName__alternateAirportName'

结果

默认情况下,搜索字段与OR组合,因此您可以添加第二个搜索字段,它将过滤匹配的对象:

class AirportLocationAdmin(admin.ModelAdmin):
...
search_fields = [
'parentAirport__officialAirportName',
'parentAirport__AirportOfficialName__alternateAirportName'
]
...

如果可以的话,我还有两个建议:

  • 在Python中,方法的属性应该是蛇形的,就像你在Admin类中看到的那样。所以我建议重命名所有模型的字段名以及related_name;定义。有关Python
  • 的官方编码风格的更多信息,请参阅PEP8。
  • "r"在你的琴弦前是过时的。这意味着您正在定义正则表达式字符串,这在这里没有意义。建议删除" ",你不需要它。

相关内容

  • 没有找到相关文章

最新更新