John Gagliardi

Python, ColdFusion, Flex, jQuery, and any other web technologies. Some Real Estate and Cooking sprinkled in also :)
Tweet to @johnnygag Follow @johnnygag

Django - Access ManyToMany objects in view Template

Here is a quick post on how to create ManyToMany relationships in Django and access the objects in view templates. 

First setup your models and add a ManyToMany relationship. Then simple call ModelName.objects.filter which will select your model and manytomany objects associated.

https://gist.github.com/4184578

Then in your view template while looping through your objects you can simple loop through the manytomany objects that were selected. In this case post.category.all represents the manytomany categories associated to the Post object.

https://gist.github.com/4184550

Short but sweet. Really liking the baked in ORM support in Django.



comments powered by Disqus