Django Rest嵌套序列化多次到多次归档错误

时间:2014-01-24 08:14:45

标签: python django serialization django-rest-framework

我遇到嵌套序列化问题(save())。

这是代码od模型:

class SingleReservation(models.Model):
    hairdos = models.ForeignKey(Hairdo, verbose_name=u'Fryzura/y z cenika')
    number = models.PositiveIntegerField(u"Liczba zamówień")

    class Meta:
        verbose_name = u'Rezerwacja dla fryzury'
        verbose_name_plural = u'Rezerwacje dla fryzury'

    def __unicode__(self):
        return u'%s' % (self.hairdos.name)


class Reservation(models.Model):
"""docstring for Reservation"""
    date = models.DateTimeField(u"Data", blank=False, null=False, validators=              [validate_date])
    adres = models.CharField(u"Adres", blank=False, null=False, max_length=1024)
    comment = models.CharField(u"Komentarz", blank=True, null=True, max_length=1024)
    time = models.FloatField(u"Liczba godzin", validators=[MinValueValidator(0.0)])
    number = models.PositiveIntegerField(u"Liczba zamówień")
    single_reservation = models.ManyToManyField(SingleReservation, verbose_name=u'Pojedyńcze zamuwienie')
    work_time = models.PositiveIntegerField(u'Szacowany czas usługi (min)')
    active = models.BooleanField(u'Potwierdzona')
    user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=u"Urzytkownik")

    class Meta:
        verbose_name = u'Rezerwacja'
        verbose_name_plural = u'Rezerwacje'

    def __unicode__(self):
        return u'%s' % (self.date)

带视图的序列化程序:

class SingleReservationSerializer(serializers.ModelSerializer):
"""Serializator do artykułów"""

    class Meta:
        model = models.SingleReservation


class ReservationSerializer(serializers.ModelSerializer):
"""Serializator do artykułów"""
    single_reservation = SingleReservationSerializer(many=True)

    class Meta:
        model = models.Reservation


class SingleReservationViewSet(viewsets.ModelViewSet):
    model = models.SingleReservation
    serializer_class = SingleReservationSerializer


class ReservationViewSet(viewsets.ModelViewSet):
    model = models.Reservation
    serializer_class = ReservationSerializer

来自请求标头的对象在提交的single_reservation中序列化:

sinlge_reservation: [{hairdos:6, number:1}]

错误(我不需要所有曲目):

Cannot add "<SingleReservation: test>": the value for field "singlereservation" is None

感谢您的帮助!

这不是最好的解决方案,但有效:

的变化:

class ReservationViewSet(viewsets.ModelViewSet):
    model = models.Reservation
    serializer_class = ReservationSerializer

    def post_save(self, obj, created=False):
        single_list = []
        for r in self.request.DATA['single_reservation']:
            reservation = models.SingleReservation(hairdos=Hairdo.objects.get(pk=r['hairdos']),
                                               number=r["number"])
            reservation.save()
            single_list.append(reservation)
        res = models.Reservation.objects.get(date=obj.date)
        res.single_reservation = single_list
        res.save()

class ReservationSerializer(serializers.ModelSerializer):
"""Serializator do artykułów"""
    single_reservation = SingleReservationSerializer(many=True)

    class Meta:
        model = models.Reservation
        exclude = ('single_reservation',)

1 个答案:

答案 0 :(得分:0)

您可以尝试将allow_add_remove=True添加到SingleReservationSerializer。

single_reservation = SingleReservationSerializer(many=True, allow_add_remove=True)

http://www.django-rest-framework.org/api-guide/serializers#dealing-with-multiple-objects中详细说明“反序列化多个对象以进行更新”

部分