Update M2M relationship django rest framework (man

2020-03-05 06:08发布

I try to update a channel:

PUT
content [{'url': 'http://localhost:8000/api/movies/2', 'title': u'Ariel', 'backdrop_path': u'/z2QUexmccqrvw1kDMw3R8TxAh5E.jpg', 'popularity': 0.082, 'poster_path': u'/8ld3BEg8gnynRsfj2AzbLocD8NR.jpg', 'release_date': datetime.date(1988, 10, 21), 'runtime': 69L, 'tagline': u'', 'vote_average': 9.0, 'vote_count': 0L}]
csrfmiddlewaretoken XXXXXXXXXXXXXXXXXXXXXXXXXXx
name    cody private
owner   http://localhost:8000/api/users/1
private 1

And I get this error:

instance should be a queryset or other iterable with many=True

And here is the code you need to understand what's going on.

class Channel(models.Model):
    """
    A channel is a "container" for a users movies and television shows.
    """
    PUBLIC_VISIBILITY, PRIVATE_VISIBILITY = 0, 1
    VISIBILITY_CHOICES = (
        (PUBLIC_VISIBILITY, 'public'),
        (PRIVATE_VISIBILITY, 'private'),
    )
    owner = models.ForeignKey(User, related_name='owned_channels')
    name = models.CharField(max_length=60)
    content = models.ManyToManyField(Movie, db_table='channel_contents',
                                     related_name='channels', null=True, blank=True, default=None)
    subscribers = models.ManyToManyField(User, db_table='channel_subscribers',
                                         related_name='subscribed_channels', null=True, blank=True, default=None)
    created = models.DateTimeField(auto_now_add=True)
    last_mod = models.DateTimeField(auto_now=True)
    query = models.CharField(max_length=255, default='')
    private = models.IntegerField(choices=VISIBILITY_CHOICES, default=PRIVATE_VISIBILITY)

    default = models.BooleanField(default=False)


class Movie(models.Model):
    id = models.BigIntegerField(primary_key=True)
    adult = models.BooleanField()
    backdrop_path = models.ImageField(upload_to='backdrop/')
    budget = models.IntegerField(blank=True, null=True)
    genres = models.ManyToManyField('Genre',
                                    through='MovieGenre',
                                    blank=True, null=True)
    homepage = models.URLField(blank=True, null=True)
    imdb_id = models.CharField(max_length=20, blank=True, null=True)
    original_title = models.CharField(max_length=100)
    overview = models.TextField(blank=True, null=True)
    popularity = models.FloatField(blank=True, null=True)
    poster_path = models.ImageField(upload_to='poster/')
    release_date = models.DateField(blank=True, null=True)
    revenue = models.IntegerField(blank=True, null=True)
    runtime = models.IntegerField(blank=True, null=True)
    tagline = models.CharField(max_length=200, blank=True, null=True)
    title = models.CharField(max_length=100, db_index=True)
    vote_average = models.FloatField(blank=True, null=True)
    vote_count = models.IntegerField(blank=True, null=True)
    actors = models.ManyToManyField('Actor',
                                    through='MovieActor',
                                    blank=True, null=True)
    directors = models.ManyToManyField('Director',
                                       through='MovieDirector',
                                       blank=True, null=True)
    production_companies = models.ManyToManyField(
        'ProductionCompany',
        through='MovieProduction',
        blank=True, null=True)

Channel serializing code:

# Routes
url(r'^channels$', ChannelList.as_view(), name='channel-list'),
url(r'^channels/(?P<pk>\d+)$', ChannelDetail.as_view(), name='channel-detail'),

# Views
class ChannelList(generics.ListCreateAPIView):
    """
    API endpoint that represents a list of users.
    """
    model = Channel
    serializer_class = ChannelSerializer

class ChannelDetail(generics.RetrieveUpdateDestroyAPIView):
    """
    API endpoint that represents a single users.
    """
    model = Channel
    serializer_class = ChannelSerializer

# Serializer
class ChannelSerializer(serializers.HyperlinkedModelSerializer):
    content = MovieSerializer(many=True)

    class Meta:
        model = Channel
        fields = ('url', 'owner', 'name', 'content', 'private')

3条回答
我只想做你的唯一
2楼-- · 2020-03-05 06:43

If you want to update the nested relation you can do like this,

      class SchoolSerializer(serializers.HyperlinkedModelSerializer):

                students = StudentSerializer(many=True, read_only=True)
                students_ids = serializers.PrimaryKeyRelatedField(many=True,\
                read_only=False, queryset=Student.objects.all(),\ 
                source='students')

                class Meta:
                    model = School
                    fields = ('name', 'image', 'address', 'url',\
                    'students', 'students_ids')

use PrimaryKeyRelatedField this will allow you to create, update, nested relations (Many-to-Many field) by just passing a list of id's students will give you nested data, students_ids can be used for write operations DRF Browsable api

查看更多
够拽才男人
3楼-- · 2020-03-05 06:49

As you can read here, nested relations currently don't support write operations. Use HyperlinkedRelatedField instead or write a custom serializer, that implements the features you need.

查看更多
来,给爷笑一个
4楼-- · 2020-03-05 06:55

This is a little outdated, but for future people looking for a potential solution to this problem, I found it useful to patch viewset.

You cannot read post params twice, which is the only thing preventing one from passing a Primary key for the related update and performing the m2m update in post_save

I made a custom viewset based on ModelViewSet with updated create and update statements:

In your app, you can create a module called viewsets.py:

# -*- coding: utf-8 -*-

from rest_framework import mixins
from rest_framework import status
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet

class RelatedCreateModelMixin(mixins.CreateModelMixin):

    '''
    Monkey patch the UpdateModel for ModelViewSet Mixin to support data
    transferrance from pre - to - save - to - post
    '''

    def create(self, request, *args, **kwargs):
        data = request.DATA
        serializer = self.get_serializer(data=data, files=request.FILES)

        if serializer.is_valid():
            self.pre_save(serializer.object, data=data)
            self.object = serializer.save(force_insert=True)
            self.post_save(self.object, created=True, data=data)
            headers = self.get_success_headers(serializer.data)
            return Response(serializer.data, status=status.HTTP_201_CREATED,
                            headers=headers)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

class RelatedUpdateModelMixin(mixins.UpdateModelMixin):

    def update(self, request, *args, **kwargs):
        partial = kwargs.pop('partial', False)
        self.object = self.get_object_or_none()

        data = request.DATA

        serializer = self.get_serializer(self.object, data=data,
                                         files=request.FILES, partial=partial)

        if not serializer.is_valid():
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

        try:
            self.pre_save(serializer.object, data=data)
        except ValidationError as err:
            # full_clean on model instance may be called in pre_save,
            # so we have to handle eventual errors.
            return Response(err.message_dict, status=status.HTTP_400_BAD_REQUEST)

        if self.object is None:
            self.object = serializer.save(force_insert=True)
            self.post_save(self.object, data=data, created=True)
            return Response(serializer.data, status=status.HTTP_201_CREATED)

        self.object = serializer.save(force_update=True)
        self.post_save(self.object, created=False)
        return Response(serializer.data, status=status.HTTP_200_OK)


class RelatedModelViewSet(RelatedCreateModelMixin,
                          mixins.RetrieveModelMixin,
                          RelatedUpdateModelMixin,
                          mixins.DestroyModelMixin,
                          mixins.ListModelMixin,
                          GenericViewSet):
    pass

Then, in your view, use instead:

from MYAPP import viewsets

Which allows you to do something along the lines of:

def post_save(self, obj, *args, **kwargs):
    data = kwargs.get('data')
    model_id = data.get('id')
    parent_obj = Model.objects.get(id=model_id)
    method = self.request.method
    if method == 'POST':
        parent_obj.m2m.add(obj)
    elif method == 'PUT':
        parent_obj.m2m.remove(obj)

Not the most elegant solution, but I find it preferable to writing a custom serializer

查看更多
登录 后发表回答