Django_filters 检查 value/connection 是否存在
Django_filters check if value/connection exists
嘿!
我正在寻找有关如何过滤的选项,如果该值存在,该值本身是无关紧要的。使用给定的 AddOn 筛选所有机构。
如果我想要我的数据库条目是否被这个变量过滤,那么我可以 'check' 的复选框是最好的选择。
如果是 'checked' 我想过滤所有具有给定变量的条目,如果没有选中我不想过滤。
models.py
class Institution(models.Model):
name = models.CharField(
verbose_name=_("Name of the institution"),
max_length=200,
)
abbreviation = models.CharField( # null=False, but "" allowed. Is Django convention
verbose_name=_("Acronym"),
max_length=25,
blank=True,
help_text=_("if applicable"),
)
class AddInstitutionMorebio(models.Model):
institution = models.OneToOneField(
Institution,
on_delete=models.PROTECT,
related_name="institution_morebio"
)
id_morebio = models.CharField(
max_length=6,
unique = True
)
filters.py
class InstitutionFilter(django_filters.FilterSet):
name = django_filters.CharFilter(method='name_filter', label="Name")
morebio_id = AddInstitutionMorebio.objects.filter(id_morebio=True) # this does nothing
有人知道该怎么做吗?
任何帮助表示赞赏! :)
如果我理解你的话,你可以将 exclude
与 exact
或 iexact
一起使用。
而不是:
.filter(id_morebio=True)
尝试:
.exclude(id_morebio__exact="")
注意,exact
前有双下划线__
。它基本上会排除所有具有 ""
作为 id_morebio
值的条目。
morebio_id = BooleanFilter(lookup_expr='isnull', field_name='institution_morebio__id_morebio', label='MoreBio')
是我找到的解决方案。就好像其他人也在为同样的事情而苦苦挣扎。
嘿!
我正在寻找有关如何过滤的选项,如果该值存在,该值本身是无关紧要的。使用给定的 AddOn 筛选所有机构。
如果我想要我的数据库条目是否被这个变量过滤,那么我可以 'check' 的复选框是最好的选择。 如果是 'checked' 我想过滤所有具有给定变量的条目,如果没有选中我不想过滤。
models.py
class Institution(models.Model):
name = models.CharField(
verbose_name=_("Name of the institution"),
max_length=200,
)
abbreviation = models.CharField( # null=False, but "" allowed. Is Django convention
verbose_name=_("Acronym"),
max_length=25,
blank=True,
help_text=_("if applicable"),
)
class AddInstitutionMorebio(models.Model):
institution = models.OneToOneField(
Institution,
on_delete=models.PROTECT,
related_name="institution_morebio"
)
id_morebio = models.CharField(
max_length=6,
unique = True
)
filters.py
class InstitutionFilter(django_filters.FilterSet):
name = django_filters.CharFilter(method='name_filter', label="Name")
morebio_id = AddInstitutionMorebio.objects.filter(id_morebio=True) # this does nothing
有人知道该怎么做吗? 任何帮助表示赞赏! :)
如果我理解你的话,你可以将 exclude
与 exact
或 iexact
一起使用。
而不是:
.filter(id_morebio=True)
尝试:
.exclude(id_morebio__exact="")
注意,exact
前有双下划线__
。它基本上会排除所有具有 ""
作为 id_morebio
值的条目。
morebio_id = BooleanFilter(lookup_expr='isnull', field_name='institution_morebio__id_morebio', label='MoreBio')
是我找到的解决方案。就好像其他人也在为同样的事情而苦苦挣扎。