django-select-related

Django : select_related with ManyToManyField

主宰稳场 提交于 2019-11-27 17:25:23
问题 I have : class Award(models.Model) : name = models.CharField(max_length=100, db_index=True) class Alias(models.Model) : awards = models.ManyToManyField('Award', through='Achiever') class Achiever(models.Model): award = models.ForeignKey(Award) alias = models.ForeignKey(Alias) count = models.IntegerField(default=1) How can I have an Alias which has all its achiever_set and awards prepopulated? >>> db.reset_queries() >>> Alias.objects.filter(id="450867").select_related("achiever_set__award")

A left outer reverse select_related in Django?

六眼飞鱼酱① 提交于 2019-11-27 03:04:29
问题 Imagine the following model: class Parent(Model): ... class Child(Model) father = ForeignKey(Parent) ... Some parents have children, others do not (they're not parents in the real meaning, it's just a fictional name). I would like to make the following query: I want to list all the Parents , and if they have children, bring me the children too . That would be the equivalent of a left outer join to Child table, that is: select * from app_parent left join app_child on child_father_id=parent_id

Optimizing database queries in Django REST framework

拈花ヽ惹草 提交于 2019-11-26 15:16:43
I have the following models: class User(models.Model): name = models.Charfield() email = models.EmailField() class Friendship(models.Model): from_friend = models.ForeignKey(User) to_friend = models.ForeignKey(User) And those models are used in the following view and serializer: class GetAllUsers(generics.ListAPIView): authentication_classes = (SessionAuthentication, TokenAuthentication) permission_classes = (permissions.IsAuthenticated,) serializer_class = GetAllUsersSerializer model = User def get_queryset(self): return User.objects.all() class GetAllUsersSerializer(serializers

Optimizing database queries in Django REST framework

浪尽此生 提交于 2019-11-26 04:19:00
问题 I have the following models: class User(models.Model): name = models.Charfield() email = models.EmailField() class Friendship(models.Model): from_friend = models.ForeignKey(User) to_friend = models.ForeignKey(User) And those models are used in the following view and serializer: class GetAllUsers(generics.ListAPIView): authentication_classes = (SessionAuthentication, TokenAuthentication) permission_classes = (permissions.IsAuthenticated,) serializer_class = GetAllUsersSerializer model = User