例如,models.py如下所示:
class Locations(models.Model):
name = models.CharField(max_length=200, null=True)
fendering_position = models.CharField(max_length=1000, null=True, blank=True)
STS_position = models.CharField(max_length=1000, null=True, blank=True)
STS_latitude = models.FloatField(null=True, blank=True)
STS_longitude = models.FloatField(null=True, blank=True)
class EmergencyContacts(models.Model):
Oil_Spill_Responders = models.CharField(max_length=1000, null=True, blank=True)
Local_Emergency_Medical_Assistance = models.CharField(max_length=1000, null=True, blank=True)
Police = models.CharField(max_length=1000, null=True, blank=True)
class Equipment_Details(models.Model):
Primary_Fenders = models.CharField(max_length=1000, null=True, blank=True)
Secondary_Fenders = models.CharField(max_length=1000, null=True, blank=True)
Fender_Moorings = models.CharField(max_length=1000, null=True, blank=True)
class LocationsForm(forms.ModelForm):
class Meta:
model = Locations
fields = [
'name', 'fendering_position', 'STS_position', 'STS_latitude', 'STS_longitude', ]
class EmergencyContactsForm(forms.ModelForm):
class Meta:
model = EmergencyContacts
fields = [
'Oil_Spill_Responders', 'Local_Emergency_Medical_Assistance', 'Police']
class Equipment_DetailsForm(forms.ModelForm):
class Meta:
model = Equipment_Details
fields = [
'Primary_Fenders', 'Secondary_Fenders', 'Fender_Moorings']
class Entry(models.Model):
locations = models.EmbeddedModelField(
model_container=(Locations),
model_form_class=LocationsForm
)
emergencycontacts = models.EmbeddedModelField(
model_container=(EmergencyContacts),
model_form_class=EmergencyContactsForm
)
equipment_details = models.EmbeddedModelField(
model_container=(Equipment_Details),
model_form_class=Equipment_DetailsForm
)现在所有内容都将存储在条目表中,所以如果我想使用django-filters进行搜索,我应该怎么做?我试着:
class EntryFilter(django_filters.FilterSet):
name = CharFilter(field_name='name', lookup_expr='icontains')
class Meta:
model = Entry.locations
fields = 'name' 这不起作用,我也试着:
class Meta:
model = Entry
fields = '__all__'而且这也不起作用,那么我应该在filters.py上写些什么才能使它能够搜索位置名称、Oil_Spill_Responders和Primary_Fenders。
发布于 2020-07-15 21:44:56
尝试此代码一次
from .models import Locations
class EntryFilter(django_filters.FilterSet):
name = CharFilter(field_name='name', lookup_expr='icontains')
class Meta:
model = Locations
fields = ["name"] 注意:当您想要传递所有字段时使用
class Meta:
model = Locations
fields = "__all__" 注意:当您传递选定字段时,请以列表形式传递它们
class Meta:
model = Locations
fields = ["field_1", "field_2", ...] https://stackoverflow.com/questions/62914219
复制相似问题