Django Model Serializer manyToMany relationship
I have problem with Django 4.1.2 (Python 3.8.9). I have 2 entities: Idea and IdeaTheme, which have manyToMany relationship.
class Idea(models.Model):
short_name = models.CharField(max_length=70)
description = models.TextField(null=True,
blank=True,
default='')
class State(models.IntegerChoices):
CREATED = 0, _("Создано")
TEAM_SEARCHING = 1, _("Поиск команды")
IN_PROCESS = 2, _("Реализация")
READY = 3, _("Готово")
state = models.IntegerField(choices=State.choices,
default=State.CREATED)
wanted_skills = models.ManyToManyField(to=UserSkill)
themes = models.ManyToManyField(to=IdeaTheme,
null=True,
blank=True)
class IdeaTheme(models.Model):
name = models.CharField(max_length=70,
verbose_name="Название")
background_photo = models.ImageField(blank=True,
null=True,
verbose_name="Изображение для фронта")
Also I have ModelSerializer
class IdeaSerializer(serializers.ModelSerializer):
class Meta:
model = Idea
fields = '__all__'
depth = 1
extra_kwargs = {
'state': {
'read_only': True
},
'pk': {
'read_only': True
}
}
And when I using CreateAPIView in order to create Idea I face the problem, that I must provide IdeaTheme pks. But in the model there are Null and Blank option and I want to create idea without IdeaThemes.
I tried to specify IdeaTheme serializer inside IdeaSerializer like this, but Django still require not empty array of IdeaTheme.
themes = IdeaThemeSerializer(many=True, required=False)
Could someone help me, please, who already has experience using built-in serializers?