Django Many to Many Relationships
wrong:
try:
user.tag_set.add(user_tag_id)
except Tag.DoesNotExist:
# This exception will not get caught due to Django's handling of M2M Relationships
correct:
if tag_id := request.data.get("tag_id", None):
if not Tag.objects.filter(id=tag_id).exists():
raise serializers.ValidationError({'tag_id': f'User tag {tag_id} not found'})
user.tag_set.add(tag_id)