1

我想知道如何根据布尔值过滤下拉列表。假设其中一个对象是True,那么它将显示,但如果是,False则不会显示。

任何帮助都会很棒。这是我的模型和管理文件的副本

模型.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']
4

1 回答 1

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)

formfield_for_foreignkey上的文档。

于 2012-11-28T22:49:02.680 回答