我想知道如何根据布尔值过滤下拉列表。假设其中一个对象是True
,那么它会被显示但是如果它是False
那么就不会显示它。
任何帮助都会很棒。这是我的模型和管理文件的副本
models.py
class Airports(models.Model):
id = models.AutoField("ID", primary_key=True, editable=False,)
airporticao = models.CharField("Airport ICAO", max_length=6, help_text="Example: CYYZ or KLGA")
airportname = models.CharField("Airport name", max_length=50, help_text="Example:")
country = models.CharField("Country", max_length=50, help_text="Example: Canada")
hub = models.BooleanField("Hub?", default=False)
个人资料模块admin.py :
class UserProfileadmin(admin.ModelAdmin):
list_display = ['user', 'country', 'vatsimid', 'totalflights', 'totalhours', 'hub']
search_fields = ['user']
答案 0 :(得分:0)
喔。试试这个:
class UserProfileadmin(admin.ModelAdmin):
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == 'hub':
kwargs['queryset'] = Airports.objects.filter(hub=True)
return super(UserProfileadmin, self).formfield_for_foreignkey(db_field, request, **kwargs)