简体   繁体   English

在一个查询集中使用两个模型进行过滤-高级搜索Django

[英]Filter with two models in one queryset - Advanced search django

Im building an advanced search form. 我正在建立一个高级搜索表单。 Im doing it with filter querysets, but I need to pass it two or more models to the form. 我用过滤器查询集来做到这一点,但我需要将其两个或多个模型传递给表单。 How can I achieve it? 我该如何实现? Until now, I have this but with some troubles: 到目前为止,我遇到了这个问题,但是有一些麻烦:

def post(self,request,*args,**kwargs):
        buscar_predio = request.POST['nombre_predio']
        matricula = request.POST['matricula_mobiliaria']
        propietario = request.POST['propietario']
        query1 = Q(nombre_predio__iexact=buscar_predio)| Q(matricula_inmobiliaria__iexact=matricula)
        query2 = Propietario.objects.filter(primer_nombre=propietario)
        predio = InfoPredioGeneral.objects.filter(query1)
        print(predio)
        if predio:
            ctx  = {'r':predio}
            return render(request,'resultados_busqueda.html',ctx)
        else:
            return render(request,'resultados_busqueda.html')

Question 1: 问题1:

query1 queryset works fine but has a little error, as you can see, it calls two fields but when I make a search by the two fields only take one, I mean in nombre_predio I put a valid query and in matricula_inmobiliaria a data that does not exist, but anyway give the result. query1查询集工作正常,但有一个小错误,你可以看到,它调用两个领域,但是当我提出由两个字段的搜索只需要一个,我的意思是在nombre_predio我把有效的查询和matricula_inmobiliaria一点不数据存在,但无论如何给出结果。 It supposed if I fill the two fields and one of two does not exists, don't has to show any result. 假设我填写了两个字段,但两个字段之一不存在,则不必显示任何结果。 How can I valid this? 我该如何验证?

Question 2: 问题2:

How can I join query2 in predio ? 如何在predio加入query2

Note: Propietario has a ForeignKey to InfoPredioGeneral. 注意:Propietario具有InfoPredioGeneral的ForeignKey。 So, I don't know if maybe to render the result in the template has to call a Lookup Field 所以,我不知道是否要在模板中呈现结果必须调用查阅字段

Here is How I render the result in the template 这是我如何在模板中呈现结果

 {% for i in r %}
        <tr>
            <td>{{i.nombre_predio}}</td>
            <td>{{i.coordenada_n}}</td>
            <td>{{i.coordenada_w}}</td>
        </tr>
{% endfor %}

My models: 我的模特:

class InfoPredioGeneral(models.Model):
    user = models.ForeignKey(User)
    fecha = models.DateField(auto_now=True)
    coordenada_n = models.DecimalField(max_digits=100,decimal_places=8,blank=True,null=True)
    coordenada_w = models.DecimalField(max_digits=100,decimal_places=8,blank=True,null=True)


class Propietario(models.Model):
    predio = models.ForeignKey(InfoPredioGeneral,blank=True,null=True,related_name='predio_propietario+')
    numero_identificacion = models.CharField(max_length=100,blank=True,null=True)
    primer_nombre = models.CharField(max_length=100)

For your query 1, you're using or, which will give you entries that validate one of the 2 conditions. 对于查询1,您使用或,它将为您提供验证两个条件之一的条目。 If you want to validate both conditions, use and 如果要验证两个条件,请使用和

 query1 = Q(nombre_predio__iexact=buscar_predio)& Q(matricula_inmobiliaria__iexact=matricula)

And I'm notre sure about the joining, but you can access foreign keys directly in the template. 我不确定是否要加入,但是您可以直接在模板中访问外键。 So if you have your proper Propietarop set, you can access InfoPredioGeneral in the template directly without another query. 因此,如果您具有正确的Propietarop设置,则可以直接在模板中访问InfoPredioGeneral,而无需其他查询。 Like this: 像这样:

<td>{{i.predio.coordenada_n}}</td>
<td>{{i.predio.coordenada_w}}</td>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM