0

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:

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. 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:

How can I join query2 in predio?

Note: Propietario has a ForeignKey to InfoPredioGeneral. 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)
dfrojas
  • 673
  • 1
  • 13
  • 32

1 Answers1

0

For your query 1, you're using or, which will give you entries that validate one of the 2 conditions. 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. Like this:

<td>{{i.predio.coordenada_n}}</td>
<td>{{i.predio.coordenada_w}}</td>
Julien Grégoire
  • 16,864
  • 4
  • 32
  • 57
  • thanks, but the solution for the join does not apply because if in the template I use i.predio.coordenada_n just show in I made a search for the model Propietario, but if I do a search for InfoPredioGeneral does not show it and viceversa. – dfrojas May 17 '15 at 23:51
  • If you want to lookup propietario from infoprediogeneral you can also access it form your template. like this: i.propietario_set.all. This'll give all propietario that have this specific infoprediogeneral as foreign key. You can scroll it also. But I'm not sure, cause in your template you have a call call to nombre_predio that isn't in your models, so this part is confusing. – Julien Grégoire May 18 '15 at 00:45