首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何让django-filters在嵌入式模型领域发挥作用?

如何让django-filters在嵌入式模型领域发挥作用?
EN

Stack Overflow用户
提问于 2020-07-15 19:52:45
回答 1查看 108关注 0票数 0

例如,models.py如下所示:

代码语言:javascript
复制
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进行搜索,我应该怎么做?我试着:

代码语言:javascript
复制
class EntryFilter(django_filters.FilterSet):
     name = CharFilter(field_name='name', lookup_expr='icontains') 

 class Meta:
        model = Entry.locations  
        fields = 'name' 

这不起作用,我也试着:

代码语言:javascript
复制
class Meta:
    model = Entry
    fields = '__all__'

而且这也不起作用,那么我应该在filters.py上写些什么才能使它能够搜索位置名称、Oil_Spill_Responders和Primary_Fenders。

EN

回答 1

Stack Overflow用户

发布于 2020-07-15 21:44:56

尝试此代码一次

代码语言:javascript
复制
from .models import Locations

class EntryFilter(django_filters.FilterSet):
     name = CharFilter(field_name='name', lookup_expr='icontains') 

     class Meta:
          model = Locations
          fields = ["name"] 

注意:当您想要传递所有字段时使用

代码语言:javascript
复制
 class Meta:
          model = Locations
          fields = "__all__" 

注意:当您传递选定字段时,请以列表形式传递它们

代码语言:javascript
复制
 class Meta:
          model =  Locations
          fields = ["field_1", "field_2", ...] 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62914219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档