django - Get the set of objects from Many To One relationship

后端 未结 2 1630
长发绾君心
长发绾君心 2021-02-07 04:01

Please have a look at these models:

class Album(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField(max_length=200)
    pub_date = mode         


        
2条回答
  •  天涯浪人
    2021-02-07 04:26

    You are almost there. you should be using photo_set instead of image_set

    >>>t_album.photo_set.all() 
    

    i.e the lowercase modelname with _set

    If you want the list of photos in 1 query,

    photos = Photo.objects.filter(album__user__username='tika')
    

提交回复
热议问题