Question

Here is my models:

class Clients(models.Model):


    client_name = models.CharField(max_lenght=100)
    commentaire_clients = models.TextField(blank=False)
    date_demande = models.TimeField(auto_now_add=True)
    choix = models.ManyToManyField('Agence', blank=True)

    def __unicode__(self):
        return unicode(self.date_demande)


class Market(models.Model):
    nom_market = models.CharField(max_length=200)
    code_postal_market = models.ManyToManyField('Lieux', blank=True)
    statut_vip = models.BooleanField(default=False)

    def __unicode__(self):
        return self.nom_market 

class Lieux(models.Model):
    code_postal = models.CharField(max_length=200)
    ville = models.CharField(max_length=200)
    region = models.CharField(max_length=200)
    departement = models.CharField(max_length=200)
    longitude = models.DecimalField(max_digits=9, decimal_places=6)
    latitude = models.DecimalField(max_digits=9, decimal_places=6)
    pays = models.CharField(max_length=100)
    def __unicode__(self):
        return unicode(self.code_postal)

Here is my view:

def comparelist(request):

    if request.session.get('code_postal'):
        poste = request.session.get('code_postal')
    else:
        poste = "RATE"

    market_match = Market.objects.filter(statut_vip=False, code_postal_market = poste)
    market_match_vip = Market.objects.filter(statut_vip=True)

   #edit bis repetita Market replace Agence models 

    return render_to_response('compare.html', {
        'code_postale': poste,
        'bien_immobilier': bien,
        'listing_agence' : market_match ,
        'listing_vip' : market_match_vip ,
    })

What I am trying to do is to make a query that will give me all the market that match: - statut_vip = False - code_postal_market = poste (that I obtain from user session from a form on the previous page

then I try to render it in my templates via:

 {% for mes_market in listing_vip %}
        <br>{{mes_market.nom_market}}
        <br>{{mes_market.statut_vip}}
{% endfor %}       

edit

here is my template for listing_agence (same as the previous one but with the right reference) sorry for the error.

 {% for mes_agences in listing_agence %}
        <br>{{mes_agences.nom_market}}
        <br>{{mes_agences.statut_vip}}

{% endfor %}       

My second queries to list all VIP clients do work but when I try to filter via the postal code given by the user via a form (and keeping the record via sessions) nothing appears.

Thank you for your help!

Was it helpful?

Solution

I finally made it!

I replaced:

market_match = Market.objects.filter(statut_vip=False, code_postal_market = poste)

by

market_match = Market.objects.filter(statut_vip=False, code_postal_market__code_postal=poste)

code_postal is from the table Lieux

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top