hajibazari
hajibazari

Reputation:

many to many problem django

how in django find list of post related with special many to many field?

for example

catagory have title

post have many to many relation to catagory

how find all post from category title

Upvotes: 1

Views: 377

Answers (3)

Mohammad Efazati
Mohammad Efazati

Reputation: 4910

give title

category = Category.objects.get(title=title)
post_list = category.post_set.all()

return result

Upvotes: 1

Andrea Zonca
Andrea Zonca

Reputation: 8783

From the category objects you can retrieve the related objects using the automatically created field post_set, if the post model is named Post.

This is a bit tricky to find in the documentation because it is in a dedicated section, see here: http://docs.djangoproject.com/en/dev/topics/db/queries/#many-to-many-relationships

Upvotes: 1

jammon
jammon

Reputation: 3464

Post.objects.all(catagory__title="My catagory title")?

Upvotes: 0

Related Questions