0% found this document useful (0 votes)
154 views

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

The document discusses filtering suppliers in a many-to-many relationship with locations in Django. It provides code examples of defining the location and supplier models with a many-to-many relationship, and filtering suppliers based on a location name passed as a parameter to the view. The answer recommends using lookups that span relationships and passing the location as a URL parameter rather than a context variable to properly filter the suppliers.

Uploaded by

ms6675
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
154 views

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

The document discusses filtering suppliers in a many-to-many relationship with locations in Django. It provides code examples of defining the location and supplier models with a many-to-many relationship, and filtering suppliers based on a location name passed as a parameter to the view. The answer recommends using lookups that span relationships and passing the location as a URL parameter rather than a context variable to properly filter the suppliers.

Uploaded by

ms6675
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

26/03/14

python - Filtering in Many to Many relationship in django - Stack Overflow


signup login tour help careers2.0

StackOverflowisaquestionandanswersiteforprofessionalandenthusiastprogrammers.It's100%free,no registrationrequired.

Takethe2minutetour

Filtering in Many to Many relationship in django

IhavetwoclassesLocationandSupplier.Asuppliersuppliesitsproductstooneormorecities.
c l a s s L o c a t i o n ( m o d e l s . M o d e l ) : c i t i e s = m o d e l s . C h a r F i e l d ( m a x _ l e n g t h = 3 0 ) c l a s s S u p p l i e r ( m o d e l s . M o d e l ) : n a m e = m o d e l s . C h a r F i e l d ( m a x _ l e n g t h = 1 0 0 ) s u p p l i e d _ c i t i e s = m o d e l s . M a n y T o M a n y F i e l d ( L o c a t i o n )

NowIhavetogetalltheSuppliersforacity,fromalistofCities.forexample:IfLondonisclicked,I shouldgettheallthesuppliersrelatedtoLondon.HowshouldIdothis?
S u p p l i e r . o b j e c t s . f i l t e r ( s u p p l i e d _ c i t i e s = 1 )

Aboveshellcommandlistsallsuppliersfromcity1(int).ButIhavetocapturetheCityNamefromthe webpageandfilterbasedonit? View:


d e f m y _ v i e w ( r e q u e s t ) : c i t y N a m e = r e q u e s t . G E T [ ' p l a c e ' ] s e l l e r s = S u p p l i e r . o b j e c t s . f i l t e r ( s u p p l i e d _ c i t i e s = L o c a t i o n . o b j e c t s . g e t ( c i t i e s = c i t y N a m e ) ) c o n t e x t = { ' s e l l e r s ' : s e l l e r s } r e t u r n r e n d e r _ t o _ r e s p o n s e ( ' r e s u l t s . h t m l ' , c o n t e x t , c o n t e x t _ i n s t a n c e = R e q u e s t C o n t e x t ( r e q u e s t ) )

Template:
{ % f o r s e l l e r s i n o b j e c t _ l i s t % } < l i > { { s e l l e r s . n a m e } } < / l i > { % e n d f o r % }
python django

editedDec13'12at10:47

askedDec13'12at7:29 Vino 77 1 8

addcomment

1 Answer
Youwanttouselookupsthatspanrelationships:
d e f m y _ v i e w ( r e q u e s t ) : c i t y _ n a m e = r e q u e s t . G E T . g e t ( ' p l a c e ' ) s e l l e r s = S u p p l i e r . o b j e c t s . f i l t e r ( s u p p l i e d _ c i t i e s _ _ c i t i e s = c i t y _ n a m e ) c o n t e x t = { ' s e l l e r s ' : s e l l e r s } r e t u r n r e n d e r _ t o _ r e s p o n s e ( ' r e s u l t s . h t m l ' , c o n t e x t , c o n t e x t _ i n s t a n c e = R e q u e s t C o n t e x t ( r e q u e s t ) )

Andthenyourtemplate:
{ % f o r s e l l e r i n s e l l e r s % } < l i > { { s e l l e r . n a m e } } < / l i > { % e n d f o r % }

Youmisnamedyourcontextvariable.

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

1/2

26/03/14

python - Filtering in Many to Many relationship in django - Stack Overflow


Youmisnamedyourcontextvariable. Ialsohighlyrecommendeitherusingdjangoformsortheurldispatcherforpassingargumentstoyour views.
u r l ( r ' ^ / s e l l e r s / ( ? P < c i t y > \ w + ) / $ ' , m y _ v i e w , n a m e = ' s e l l e r s ' ) d e f m y _ v i e w ( r e q u e s t , c i t y ) : # r e s t o f y o u r v i e w
editedDec13'12at11:05 answeredDec13'12at10:57 JoshSmeaton 17k 11 55 105

Thanks.Filterworksinshellcommand.HowshouldIcreatetheURL?Forexample,currentlyIamusing 127.0.0.1:8000/locations/search/londonbutitlookslikecontextvariable'sellers'isalwaysempty. Vino Dec14'12at6:45 Thanks.Itworked. Vino Dec21'12at8:44 addcomment

Not the answer you're looking for? Browse other questions tagged python django or ask your own question.

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

2/2

You might also like