How to get rid of similar queries that are shown in django-toolbar?

I have this serializer

class CurrentUserSerializer(serializers.ModelSerializer):
        group_list = serializers.SerializerMethodField()

        def get_group_list(self, instance):
            group_list = instance.group_list.all().select_related("django_group")
            if group_list:
                for group in group_list:
                    print(group.django_group.permissions.values("codename"))
            return None

        class Meta:
            model = User
            fields = [
                "group_list",
            ]

with this group model

class Group(AbstractModel):
    name = models.CharField(max_length=150)
    django_group = models.OneToOneField(
        DjangoGroup, related_name="group", on_delete=models.CASCADE
    )
    users = models.ManyToManyField(
        User, related_name="group_list", db_table="Group_User", blank=True
    )

    def __str__(self):
        return self.name

and the django group model

class Group(models.Model):
    name = models.CharField(_("name"), max_length=150, unique=True)
    permissions = models.ManyToManyField(
        Permission,
        verbose_name=_("permissions"),
        blank=True,
    )

    objects = GroupManager()

    class Meta:
        verbose_name = _("group")
        verbose_name_plural = _("groups")

    def __str__(self):
        return self.name

    def natural_key(self):
        return (self.name,)

I want to get the user permissions from django group but without re-exuting the query many times, this is what django-toolbar is showing me django-toolbar-screenshot

Back to Top