Python - Filtering in Many to Many Relationship in Django - Stack Overflow

2
26/03/14 python - Filtering in Many to Many relationship in django - Stack Overflow stackoverflow.com/questions/13855033/filtering-in-many-to-many-relationship-in-django 1/2 Take the 2minute tour × Vino 77 1 8 1 Answer I have two classes Location and Supplier. A supplier supplies its products to one or more cities. class Location(models.Model): cities = models.CharField(max_length=30) class Supplier(models.Model): name = models.CharField(max_length=100) supplied_cities = models.ManyToManyField(Location) Now I have to get all the Suppliers for a city, from a list of Cities. for example: If London is clicked, I should get the all the suppliers related to London. How should I do this? Supplier.objects.filter(supplied_cities= 1) Above shell command lists all suppliers from city 1 (int). But I have to capture the City Name from the web page and filter based on it? View: def my_view(request): cityName = request.GET['place'] sellers = Supplier.objects.filter(supplied_cities= Location.objects.get(cities context ={'sellers' : sellers } return render_to_response('results.html',context,context_instance=RequestConte Template: {% for sellers in object_list %} <li> {{ sellers.name }} </li> {% endfor %} python django edited Dec 13 '12 at 10:47 asked Dec 13 '12 at 7:29 You want to use lookups that span relationships: def my_view(request): city_name = request.GET.get('place') sellers = Supplier.objects.filter(supplied_cities__cities=city_name) context ={'sellers' : sellers } return render_to_response('results.html', context, context_instance=RequestCon And then your template: {% for seller in sellers %} <li> {{ seller.name }} </li> {% endfor %} You misnamed your context variable. Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required. Filtering in Many to Many relationship in django sign up log in tour help careers 2.0 add comment

Transcript of Python - Filtering in Many to Many Relationship in Django - Stack Overflow

Page 1: Python - Filtering in Many to Many Relationship in Django - Stack Overflow

26/03/14 python - Filtering in Many to Many relationship in django - Stack Overflow

stackoverflow.com/questions/13855033/filtering-in-many-to-many-relationship-in-django 1/2

Take the 2­minute tour   ×

Vino77 1 8

1 Answer

I have two classes Location and Supplier. A supplier supplies its products to one or more cities.

class Location(models.Model):    cities = models.CharField(max_length=30)

class Supplier(models.Model):    name = models.CharField(max_length=100)    supplied_cities = models.ManyToManyField(Location)

Now I have to get all the Suppliers for a city, from a list of Cities. for example: If London is clicked, Ishould get the all the suppliers related to London. How should I do this?

Supplier.objects.filter(supplied_cities= 1)

Above shell command lists all suppliers from city 1 (int). But I have to capture the City Name from theweb page and filter based on it?

View:

def my_view(request):    cityName = request.GET['place']    sellers = Supplier.objects.filter(supplied_cities= Location.objects.get(cities=cityName))    context = {'sellers' : sellers }    return render_to_response('results.html',context,context_instance=RequestContext(request))

Template:

{% for sellers in object_list %}<li>  {{ sellers.name }} </li>{% endfor %}

python   django

edited Dec 13 '12 at 10:47 asked Dec 13 '12 at 7:29

You want to use lookups that span relationships:

def my_view(request):    city_name = request.GET.get('place')    sellers = Supplier.objects.filter(supplied_cities__cities=city_name)    context = {'sellers' : sellers }    return render_to_response('results.html', context, context_instance=RequestContext(request))

And then your template:

{% for seller in sellers %}    <li>  {{ seller.name }} </li>{% endfor %}

You misnamed your context variable.

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, noregistration required.

Filtering in Many to Many relationship in django

sign up 

log in 

tour 

help  

careers 2.0 

add comment

Page 2: Python - Filtering in Many to Many Relationship in Django - Stack Overflow

26/03/14 python - Filtering in Many to Many relationship in django - Stack Overflow

stackoverflow.com/questions/13855033/filtering-in-many-to-many-relationship-in-django 2/2

Josh Smeaton17k 11 55 105

You misnamed your context variable.

I also highly recommend either using django forms or the url dispatcher for passing arguments to yourviews.

url(r'̂/sellers/(?P<city>\w+)/$', my_view, name='sellers')

def my_view(request, city):    # rest of your view

edited Dec 13 '12 at 11:05 answered Dec 13 '12 at 10:57

    Thanks. Filter works in shell command. How should I create the URL? For example, currently I am using127.0.0.1:8000/locations/search/london but it looks like context variable 'sellers' is always empty. –   VinoDec 14 '12 at 6:45

    Thanks. It worked. –   Vino  Dec 21 '12 at 8:44

Not the answer you're looking for? Browse other questions tagged python django or

ask your own question.

add comment