Django Rest Framework - 可以保存数字值为

时间:2017-06-14 15:05:03

标签: django django-rest-framework

我尝试序列化并保存以下json

{
    "start": "2017-12-12",
    "end": "2017-12-12",
    "has_refund": false,
    "room": {
        "key": 0
    },
    "reserved_days": [ {
       "date": "2017-12-12",
       "price": "2",
       "paymentMethod": "3"
    },
    {
        "date": "2017-12-13",
        "price": "2",
        "paymentMethod": "3"
    }
    ],
    "check_in_time": "14:00",
    "check_out_time": "12:00",
    "guest_name": "Ivan",
    "payed": false
}

串行

class DaySerializer(serializers.ModelSerializer):
    class Meta:
        model = Day
        fields = [
            'date',
            'price',
            'paymentMethod',
        ]

class RoomSerializer(serializers.ModelSerializer):
    class Meta:
        model = Room
        fields = [
            'key',
        ]

class ReservationSerializer(serializers.ModelSerializer):
    room = RoomSerializer()
    reserved_days = DaySerializer(many=True)

    class Meta:
        model = Reservation
        fields = [
            'start',
            'end',
            'check_in_time',
            'check_out_time',
            'reserved_days',
            'room',
            'has_refund',
            'payed',
            'guest_name',
            'reservation_number',
        ]

    def create(self, validated_data):
        day_data = validated_data.pop('reserved_days')
        room = validated_data.pop('room')

        reservation = Reservation.objects.create(**validated_data)
        existing_room = Room.objects.get(key=room['key'])
        reservation.room = existing_room
        reservation.save()

        for day in day_data:
            day, created = Day.objects.get_or_create(date=day['date'], price=day['price'], paymentMethod=day['paymentMethod'])
            reservation.reserved_days.add(day)
        return reservation

MODEL

class Day(models.Model):
    date = models.DateField(auto_now=False, auto_now_add=False)
    price = models.FloatField()
    paymentMethod = models.CharField(max_length = 200)

    def __unicode__(self):
        return str(self.date)

class Room(models.Model):
    name = models.CharField(max_length = 200, null=True)
    key = models.AutoField(primary_key=True)

    def __unicode__(self):
        return self.name

class Reservation(models.Model):
    start = models.DateField(verbose_name='Заезд', auto_now=False, auto_now_add=False, blank=False)
    end = models.DateField(verbose_name='Выезд', auto_now=False, auto_now_add=False, blank=False)
    check_in_time = models.TimeField(verbose_name='Время заезда', blank=False)
    check_out_time = models.TimeField(verbose_name='Время выезда', blank=False)
    has_refund = models.BooleanField(verbose_name='Возвратная бронь', default=True)
    payed = models.BooleanField(verbose_name='Оплачено', default=False)
    room = models.ForeignKey(Room, null=True, blank=True, verbose_name='Номер', on_delete=models.CASCADE)
    reserved_days = models.ManyToManyField(Day, blank=False)
    guest_name = models.CharField(verbose_name='Имя гостя', max_length=200, blank=True)
    reservation_number = models.CharField(verbose_name='Номер брони', max_length=200, blank=True)

作为回应,我得到500错误,这个

  

/ core / create /的KeyError   '钥匙'

但是,如果我将房间的字段更改为name(房间序列化程序和预约序列化程序的create方法),则一切正常

class RoomSerializer(serializers.ModelSerializer):
    class Meta:
        model = Room
        fields = [
            'name',
        ]

class ReservationSerializer(serializers.ModelSerializer):
    room = RoomSerializer()
    reserved_days = DaySerializer(many=True)

    class Meta:
        model = Reservation
        fields = [
            'start',
            'end',
            'check_in_time',
            'check_out_time',
            'reserved_days',
            'room',
            'has_refund',
            'payed',
            'guest_name',
            'reservation_number',
        ]

    def create(self, validated_data):
        day_data = validated_data.pop('reserved_days')
        room = validated_data.pop('room')

        reservation = Reservation.objects.create(**validated_data)
        existing_room = Room.objects.get(name=room['name'])
        reservation.room = existing_room
        reservation.save()

        for day in day_data:
            day, created = Day.objects.get_or_create(date=day['date'], price=day['price'], paymentMethod=day['paymentMethod'])
            reservation.reserved_days.add(day)
        return reservation

似乎它只适用于字符串。 如何使它与数字一起工作?当我尝试使用pk而不是分隔的int fieled

进行保存时,我发生了同样的事情

0 个答案:

没有答案