Answer the question
In order to leave comments, you need to log in
How to check the existence of an M2M relation??
models:
class User(models.Model):
...
group = models.ManyToManyField(
Group, related_name="users_group", verbose_name="The User Group"
)
class Group(models.Model):
....
User
) class GroupSerializer(serializers.ModelSerializer):
empty = serializers.SerializerMethodField()
class Meta:
...
def get_empty(self, obj):
return not User.objects.filter(group=obj).exists()
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question