如何使用 Djangorestframework 中的 UpdateAPIView 更新模型中的单个字段?
How to update a single field in a model using UpdateAPIView from Djangorestframework?
我正在学习通用视图并创建一些 Api。如何更新字段:"mobile" 来自模型:"Contacts"?
我想从 url(mobile/update/user_id) 获取用户 ID。但是在创建查询集时它不起作用。我想做一些像这里提到的事情(#queryset = Contacts.objects.filter(id=Usertab.objects.filter(id=self.kwargs['id']).first().contact.id))
'''python
class UpdateMobileAPIView(generics.UpdateAPIView):
queryset = Contacts.objects.filter(pk=Usertab.objects.all())
serializer_class = ContactsSerializer
lookup_field = 'pk'
def update(self,instance,request):
instance = self.get_object()
serializer= self.get_serializer(instance,data=request.data,partial=True)
if serializer.is_valid():
serializer.save()
return Response({"message":"mobile number updated successfully"})
else:
return Response({"message":"failed"})
'''
这些是模型
class Contacts(models.Model):
mobile = models.IntegerField(null=False)
Landline = models.IntegerField(null=False)
whats_app = models.IntegerField(null=False)
class 用户选项卡(models.Model):
username = models.CharField(max_length=255,null=False,blank=False)
address = models.CharField(max_length=255,null=False,blank=False)
pin_code = models.CharField(max_length=255,null=False,blank=False)
contact = models.ForeignKey(Contacts,related_name="contacts_user")
class 邮箱(models.Model):
user = models.ForeignKey(Usertab,related_name="user_email")
email = models.CharField(max_length=255,null=False,blank=False)
is_verified = models.BooleanField(default=False)
'''
'''这是序列化器
class ContactsSerializer(ModelSerializer):
class Meta:
model = Contacts
fields = '__all__'
def update(self, instance, validated_data):
instance.mobile = validated_data.get('mobile', instance.mobile)
instance.save()
return instance
'''
TypeError: update() 得到了一个意外的关键字参数 'pk'
我认为错误与您的 queryset
值有关。如果目标是
联系人模型,只需将查询集作为联系人模型中的所有项目。这样,查找字段可用于过滤整个数据集并编辑适当的条目。
class UpdateMobileAPIView(generics.UpdateAPIView):
queryset = Contacts.objects.all()
serializer_class = ContactsSerializer
lookup_field = 'pk'
def update(self, request, *args, **kwargs):
instance = self.get_object()
serializer = self.get_serializer(instance, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response({"message": "mobile number updated successfully"})
else:
return Response({"message": "failed", "details": serializer.errors})
我正在学习通用视图并创建一些 Api。如何更新字段:"mobile" 来自模型:"Contacts"?
我想从 url(mobile/update/user_id) 获取用户 ID。但是在创建查询集时它不起作用。我想做一些像这里提到的事情(#queryset = Contacts.objects.filter(id=Usertab.objects.filter(id=self.kwargs['id']).first().contact.id))
'''python
class UpdateMobileAPIView(generics.UpdateAPIView):
queryset = Contacts.objects.filter(pk=Usertab.objects.all())
serializer_class = ContactsSerializer
lookup_field = 'pk'
def update(self,instance,request):
instance = self.get_object()
serializer= self.get_serializer(instance,data=request.data,partial=True)
if serializer.is_valid():
serializer.save()
return Response({"message":"mobile number updated successfully"})
else:
return Response({"message":"failed"})
''' 这些是模型
class Contacts(models.Model):
mobile = models.IntegerField(null=False)
Landline = models.IntegerField(null=False)
whats_app = models.IntegerField(null=False)
class 用户选项卡(models.Model):
username = models.CharField(max_length=255,null=False,blank=False)
address = models.CharField(max_length=255,null=False,blank=False)
pin_code = models.CharField(max_length=255,null=False,blank=False)
contact = models.ForeignKey(Contacts,related_name="contacts_user")
class 邮箱(models.Model):
user = models.ForeignKey(Usertab,related_name="user_email")
email = models.CharField(max_length=255,null=False,blank=False)
is_verified = models.BooleanField(default=False)
'''
'''这是序列化器
class ContactsSerializer(ModelSerializer):
class Meta:
model = Contacts
fields = '__all__'
def update(self, instance, validated_data):
instance.mobile = validated_data.get('mobile', instance.mobile)
instance.save()
return instance
'''
TypeError: update() 得到了一个意外的关键字参数 'pk'
我认为错误与您的 queryset
值有关。如果目标是
联系人模型,只需将查询集作为联系人模型中的所有项目。这样,查找字段可用于过滤整个数据集并编辑适当的条目。
class UpdateMobileAPIView(generics.UpdateAPIView):
queryset = Contacts.objects.all()
serializer_class = ContactsSerializer
lookup_field = 'pk'
def update(self, request, *args, **kwargs):
instance = self.get_object()
serializer = self.get_serializer(instance, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response({"message": "mobile number updated successfully"})
else:
return Response({"message": "failed", "details": serializer.errors})