我有通过 OneToOneRelation 扩展用户模型的观察者模型。我对具有 Observer 外键的模型清单进行了过滤。这是models.py代码:
class Observer(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
rating = models.IntegerField(default=0)
bird_ratings = models.ManyToManyField('BirdName', through='BirdRatings')
class Checklist(gismodels.Model):
date = models.DateField()
time_start = models.TimeField(null=True)
time_end = models.TimeField(null=True)
site_name = models.CharField(max_length=200)
municipality = models.CharField(max_length=200)
observer = models.ForeignKey(Observer, null=True, default='')
other_observers = models.CharField(max_length=150, default='')
note = models.CharField(max_length=2000)
location_note = models.CharField(max_length=2000, default='')
position = gismodels.PointField(null=True, srid=4326)
created = models.DateTimeField(db_index=True, null=True)
rating = models.IntegerField(default=0)
然后我在 filters.py 中创建了 ChecklistFilter 类:
class ChecklistFilter(django_filters.FilterSet):
date = DateFromToRangeFilter(label='Datum pozorování', widget=RangeWidget(attrs={'display': 'inline'}))
#groups = django_filters.ModelMultipleChoiceFilter(queryset=User.objects.filter(), widget=forms.CheckboxSelectMultiple)
created = DateFilter(label='Datum vložení')
observer = ModelChoiceFilter(queryset=Observer.objects.filter(), label='Pozorovatel')
municipality = CharFilter(label='Obec')
oblast = CharFilter(label='Oblast')
rating = NumberFilter(label='Hodnocení')
class Meta:
model = Checklist
fields = ['date', 'created', 'observer', 'municipality', 'site_name', 'rating']
这是我的模板代码的一部分,我在其中使用观察者字段:
<div class="col-sm-2">
{{ filter.form.observer.label_tag }}
{% render_field filter.form.observer class="form-control" %}
</div>
但问题是我不需要获取观察者对象,但我需要来自与观察者模型相关联的用户模型的 last_name 。在表单中,我只是扩展 ModelChoiceField 并覆盖 label_from_instance() 方法。但这在这里不起作用。Django-filter 使用这些类,但覆盖在这里不起作用。我试过这个:
from .models import Checklist, Observer
from django.contrib.auth.models import User
import django_filters
from django_filters import DateFromToRangeFilter, DateFilter, CharFilter, NumberFilter, ModelChoiceFilter
from django_filters.widgets import RangeWidget
from django import forms
class ChecklistModelChoiceField(forms.ModelChoiceField):
def label_from_instance(self, obj):
return obj.user.last_name
class ChecklistFilter(django_filters.FilterSet):
date = DateFromToRangeFilter(label='Datum pozorování', widget=RangeWidget(attrs={'display': 'inline'}))
#groups = django_filters.ModelMultipleChoiceFilter(queryset=User.objects.filter(), widget=forms.CheckboxSelectMultiple)
created = DateFilter(label='Datum vložení')
observer = ChecklistModelChoiceField(queryset=Observer.objects.filter(), label='Pozorovatel')
municipality = CharFilter(label='Obec')
oblast = CharFilter(label='Oblast')
rating = NumberFilter(label='Hodnocení')
class Meta:
model = Checklist
fields = ['date', 'created', 'observer', 'municipality', 'site_name', 'rating']
编辑
是的,我都试过了。然后我尝试了:
class ChecklistModelChoiceField(django_filters.ModelChoiceFilter):
def label_from_instance(self, obj):
return obj.user.last_name
没有错误,但label_from_instance()
没有使用。仍在选择中的观察者对象。
我犯了一个错误。ModelChoiceFilter 不扩展 ChoiceField。过滤器中的代码如下所示。它只是财产:
class ModelChoiceFilter(QuerySetRequestMixin, Filter):
field_class = forms.ModelChoiceField
所以我的第一个想法是行不通的。还有其他机会如何制作我需要的东西吗?