gpt4 book ai didi

python - Viewset 的 Django 更新方法即使在覆盖后仍返回 415

转载 作者:太空宇宙 更新时间:2023-11-03 19:55:36 24 4
gpt4 key购买 nike

现在我正在学习使用 Django 创建 REST API,但正在努力实现 Update 方法。我的测试总是失败,我不确定这是因为序列化器还是其他原因。希望对我的代码有一些快速反馈!

class Company(models.Model):
owner = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=50)

class JobPost(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
company = models.ForeignKey(Company, on_delete=models.CASCADE, related_name='guest_posts')
heading = models.CharField(max_length=100, default="")

class CompanySerializer(serializers.ModelSerializer):
class Meta:
model = Company
fields = ('name')

class JobPostSerializer(serializers.ModelSerializer):
company = CompanySerializer(read_only=True, required=False)

class Meta:
model = GuestPost
fields = ('id', 'heading', 'company')

def create(self, validated_data):
request = self.context.get('request')
company = Company.objects.get(title=request.data.get('company_name'))
return GuestPost.objects.create(owner=request.user,
heading=validated_data.get('heading'),
company=company)

def update(self, instance, validated_data):
instance.heading = validated_data.pop('heading')
instance.save()
return instance

class JobPostViewSet(viewsets.ViewSet,
viewsets.generics.ListCreateAPIView,
viewsets.mixins.UpdateModelMixin,
viewsets.mixins.RetrieveModelMixin):
permission_classes = (JobPostPermissions,)
serializer_class = JobPostSerializer
queryset = JobPost.objects.all()

然后我测试此代码,其他操作(创建、获取、列表)的所有测试都有效,但在使用以下方法测试更新时收到错误 415:

class Test(TestCase):

def setUp(self):
self.user = User.objects.create(username="elon")
self.factory = RequestFactory()
self.request_url = '/api/job-post/'
self.boring_company = Company.objects.create(owner=self.user, title="Boring Company")
self.job_post = JobPost.objects.create(company=self.boring_company, heading="Looking for boring person")
self.sample_uuid = self.job_post.id

def test_update_post(self):
data = {
'id': self.sample_uuid,
'heading': 'New Heading',
'description': 'new description',
}
request = self.factory.put(self.request_url, data=data)
view = GuestPostViewSet.as_view({'put': 'update'})
force_authenticate(request, user=self.user)
response = view(request, pk=self.sample_uuid)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['heading'], 'New Heading')

提前致谢!

最佳答案

我在第二次查看 Django Rest docs 后偶然发现了答案。 。正如其他人指出的那样,问题出在测试上。以下是更新请求所需执行的官方示例:

from django.test.client import encode_multipart, RequestFactory

factory = RequestFactory()
data = {'title': 'remember to email dave'}
content = encode_multipart('BoUnDaRyStRiNg', data)
content_type = 'multipart/form-data; boundary=BoUnDaRyStRiNg'
request = factory.put('/notes/547/', content, content_type=content_type)

关于python - Viewset 的 Django 更新方法即使在覆盖后仍返回 415,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59556732/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com