python - django return foreign key object from queryset? -


so have 3 models

class post(....  class project(....  # have many many relationship class projectpost(....     post = ..... # foreignkey     project = .... # foreignkey 

the data set want select list of post objects given project object.

this tried:

posts_list = projectpost.objects.filter(project=project_object).select_related("post") 

but returns list of projectpost objects rather list of post objects. correct way of doing this?

you may want use manytomanyfield()

https://docs.djangoproject.com/en/dev/topics/db/examples/many_to_many/

you should this:

class post(models.model):     pass   class project(models.model):     posts = models.manytomanyfield(post) 

and then, if want access posts of project, can do

project_obj.posts.all() 

you can use queryset methods

if want access projects of posts can do

post_obj.project_set.all() 

same before, can use queryset methods.

if reason want way, do:

post_list = projectpost.objects.filter(project=project_object).values('post') 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -