[Django]-Django: follow relations backwards

5👍

galleries = Galleries.objects.get(id=1)
values = galleries.gallery_type.values_set.filter(language='language')

Interestingly, you used the exact wording that the docs use to refer to the related field lookups. I always found the definition strange to the gut, maybe because they put it in quotes.

FOLLOWING RELATIONSHIPS “BACKWARD”

http://docs.djangoproject.com/en/1.2/topics/db/queries/#following-relationships-backward

1👍

You may want to use the select_related method of objects so you reduce the number of queries you are making. select_related

gallery = Galleries.objects.select_related().get(id=1)

You can set a related name for the Values model in the category fk:

class Values(models.Model):
  category = models.ForeignKey(Categories, related_name="categories")
  language = models.CharField(max_length=7)
  category_name = models.CharField(max_length=50)

now you can get your list of values for a specific language by doing

values = gallery.gallery_type.categories.filter(language="language")
👤dting

Leave a comment