Django rest framework создает объект с сериализатором файлов "многие ко многим

я не могу создать объект проекта с полем многие-ко-многим в модели языков, мне нужен лучший способ реализовать это, я пробовал django rest framework docs но это не помогло мне, нужно ли создавать объекты языков до объекта проекта или DRF будет обрабатывать это? затем я попробовал Direct assignment to the forward side of a many-to-many set is prohibited. Use languages.set() instead. steal not worklanguages.set()

models.py


from django.db import models
from django.contrib.auth.models import User


class Language(models.Model):

    name = models.CharField(max_length=100, blank=True, default='')
    description = models.TextField()
    owner = models.ForeignKey(User, on_delete=models.CASCADE)

    class Meta:
        ordering = ['id']

    def __str__(self):
        return self.name


class Developer(models.Model):

    name = models.CharField(max_length=100, blank=True, default='')
    age = models.IntegerField()
    bio = models.TextField()
    created = models.DateTimeField(auto_now_add=True)
    owner = models.ForeignKey(User, on_delete=models.CASCADE)

    class Meta:
        ordering = ['created']

    def __str__(self):
        return self.name


class Project(models.Model):

    name = models.CharField(max_length=100, blank=True, default='')
    stars = models.IntegerField()
    developer = models.ForeignKey(Developer, related_name='projects', on_delete=models.CASCADE)
    languages = models.ManyToManyField(Language)
    owner = models.ForeignKey(User, on_delete=models.CASCADE)

    class Meta:
        ordering = ['stars']

    def __str__(self):
        return self.name

serializers.py


from rest_framework import serializers

from developers.models import Language, Developer, Project


class ProjectSerializer(serializers.ModelSerializer):

    owner = serializers.ReadOnlyField(source='owner.username')

    class Meta:
        model = Project
        fields = ['id', 'name', 'stars', 'developer', 'languages', 'owner', ]

    def create(self, validated_data):
        project = Project.objects.create(owner=self.context['request'].user, **validated_data)
        return project


class LanguageSerializer(serializers.ModelSerializer):

    owner = serializers.ReadOnlyField(source='owner.username')
    projects = ProjectSerializer(read_only=True, many=True)

    class Meta:
        model = Language
        fields = ['id', 'name', 'description', 'projects', 'owner', ]

    def create(self, validated_data):
         language = Language.objects.create(owner=self.context['request'].user, **validated_data)
         return language


class DeveloperSerializer(serializers.ModelSerializer):

    owner = serializers.ReadOnlyField(source='owner.username')
    projects = ProjectSerializer(read_only=True, many=True)

    class Meta:
        model = Developer
        fields = ['id', 'name', 'age', 'bio', 'projects', 'owner', ]

    def create(self, validated_data):
         developer = Developer.objects.create(owner=self.context['request'].user, **validated_data)
         return developer
Вернуться на верх