Django Rest Framework更新不保存更改

时间:2018-05-09 01:02:29

标签: python django django-rest-framework

我有一个通过一对一字段相关的两个模型,我想创建一个REST服务来管理它们。到目前为止,post完美地工作,并且在两个模型中创建新实例,因为它们在一个模型中,但put方法什么都不做。它不会引发任何错误或任何错误,只会保持数据不变。

这些是我的模特:

class Company(models.Model):
    name = models.CharField(max_length=40)
    legal_name = models.CharField(max_length=100)
    url = models.URLField()
    address = models.TextField(max_length=400)

    def __str__(self):
        return "[{}]{}".format(self.id, self.name)

class Hotel(models.Model):
    company = models.OneToOneField('Company', on_delete=models.CASCADE, primary_key=True)
    code = models.CharField(max_length=10)
    city = models.ForeignKey('City', on_delete=models.PROTECT, related_name='city_hotels')
    category = models.ForeignKey('HotelCategory', on_delete=models.PROTECT, related_name='category_hotels')
    capacity = models.IntegerField()
    position = models.DecimalField(max_digits=11, decimal_places=2, default=0.00)
    in_pickup = models.BooleanField(default=False)

    def __str__(self):
        return self.company.name

这是我的ViewSet:

class HotelViewSet(viewsets.ModelViewSet):
    queryset = models.Hotel.objects.all()
    serializer_class = serializers.HotelSerializer

这些是我的序列化器:

class CompanySerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = models.Company
        fields = ('id', 'name', 'legal_name', 'url', 'address')

class HotelSerializer(serializers.HyperlinkedModelSerializer):
    company = CompanySerializer()
    class Meta:
        model = models.Hotel
        fields = ('company', 'code', 'city', 'category', 'capacity', 'position', 'in_pickup')

    def create(self, validated_data):
        company_data = validated_data.pop('company')
        new_company=models.Company.objects.create(**company_data)
        hotel = models.Hotel.objects.create(company=new_company, **validated_data)

        return hotel

    def update(self, instance, validated_data):

        company = models.Company(
            id=instance.company.id,
            name=instance.company.name,
            legal_name=instance.company.legal_name,
            url=instance.company.url,
            address=instance.company.address
        )

        company.save()
        instance.save()

       return instance

2 个答案:

答案 0 :(得分:0)

您正在创建新的公司实例,而不是更新属于酒店实例的实例。

company = Company.objects.get(id=instance.company.id)
company.name = instance.company.name
company.legal_name = instance.company.name
company.url = instance.company.url
company.address = instance.company.address
company.save()
instance.save()

答案 1 :(得分:0)

我发现validated_data包含原始数据,instance包含新数据。我正在保存原始数据。

我必须将validated_data数据替换为instance数据,然后保存def update(self, instance, validated_data): company_data = validated_data.pop('company') company = models.Company.objects.get(id=instance.company.id) company.name = company_data.get('name') company.legal_name = company_data.get('legal_name') company.tax_id = company_data.get('tax_id') company.url = company_data.get('url') company.address = company_data.get('address') instance.company = company instance.code = validated_data.get('code', instance.code) instance.city = validated_data.get('city', instance.city) instance.category = validated_data.get('category', instance.category) instance.capacity = validated_data.get('capacity', instance.capacity) instance.position = validated_data.get('position', instance.position) instance.in_pickup = validated_data.get('in_pickup', instance.in_pickup) instance.is_active = validated_data.get('is_active', instance.is_active) company.save() instance.save() return instance

put

function [Q] = reference_configuration(left, right, n, t) l = left(t); r = right(t); diff = r - l; x = linspace(0, diff, n); y = zeros(1, n); q = zeros(1, 2*n); q(1:2:end) = x; q(2:2:end) = y; end 方法现在很有效。