Cyber-Physical Systems #8177
Unanswered
schienieder
asked this question in
Potential Issue
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I am a beginner in Django & DRF and I have a very dumb problem with my project, in which it doesn't allow me to do partial update. I am using generic views (UpdateAPIView) and I have been stuck with it for 2 weeks now.
It allows me to update, however I have to fill every field but what I want to do is to pop email & mobile number if they are the same as the stored value in the database.
Hoping someone might help and thank you in advance.
Model:
`class BusinessPartner(models.Model):
first_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=150)
mobile_number = models.CharField(max_length=150, unique=True)
email = models.EmailField(max_length=150, unique=True)
business_name = models.CharField(max_length=255, blank=True)
type_of_business = models.CharField(max_length=150, blank=True)
street_address = models.CharField(max_length=255, blank=True)
city = models.CharField(max_length=150, blank=True)
state_province = models.CharField(max_length=150, blank=True)
postal_zip = models.IntegerField(null=True, blank=True)
services_offered = models.TextField(null=True, blank=True)
account = models.ForeignKey(Account, on_delete=models.CASCADE)
class Meta:
ordering = ["-account"]
def str(self):
return self.first_name + " " + self.last_name`
Serializer:
`class BusinessPartnerSerializer(serializers.ModelSerializer):
class Meta:
model = BusinessPartner
fields = [
"id",
"first_name",
"last_name",
"mobile_number",
"email",
"business_name",
"type_of_business",
"street_address",
"city",
"state_province",
"postal_zip",
"services_offered",
"account",
]
extra_kwargs = {"id": {"read_only": True}}
def update(self, instance, validated_data):
partner = BusinessPartner.objects.get(account=instance)
print("Previous mobile number: ", getattr(partner, "mobile_number"))
print("New mobile number: ", validated_data["mobile_number"])
I am a beginner in Django & DRF and I have a very dumb problem with my project, in which it doesn't allow me to do partial update. I am using generic views (UpdateAPIView) and I have been stuck with it for 2 weeks now.
It allows me to update, however I have to fill every field but what I want to do is to pop email & mobile number if they are the same as the stored value in the database.
Hoping someone might help and thank you in advance.
Model:
class BusinessPartner(models.Model):
first_name = models.CharField(max_length=150)
last_name = models.CharField(max_length=150)
mobile_number = models.CharField(max_length=150, unique=True)
email = models.EmailField(max_length=150, unique=True)
business_name = models.CharField(max_length=255, blank=True)
type_of_business = models.CharField(max_length=150, blank=True)
street_address = models.CharField(max_length=255, blank=True)
city = models.CharField(max_length=150, blank=True)
state_province = models.CharField(max_length=150, blank=True)
postal_zip = models.IntegerField(null=True, blank=True)
services_offered = models.TextField(null=True, blank=True)
account = models.ForeignKey(Account, on_delete=models.CASCADE)
class Meta:
ordering = ["-account"]
def str(self):
return self.first_name + " " + self.last_name
Serializer:
class BusinessPartnerSerializer(serializers.ModelSerializer):
class Meta:
model = BusinessPartner
fields = [
"id",
"first_name",
"last_name",
"mobile_number",
"email",
"business_name",
"type_of_business",
"street_address",
"city",
"state_province",
"postal_zip",
"services_offered",
"account",
]
extra_kwargs = {"id": {"read_only": True}}
def update(self, instance, validated_data):
partner = BusinessPartner.objects.get(account=instance)
print("Previous mobile number: ", getattr(partner, "mobile_number"))
print("New mobile number: ", validated_data["mobile_number"])
View:
`class UpdatePartnerProfileView(generics.UpdateAPIView):
permission_classes = [IsAuthenticated]
serializer_class = BusinessPartnerSerializer
queryset = BusinessPartner.objects.all()
def update(self, request, *args, **kwargs):
serializer = self.serializer_class(
request.user, data=request.data, partial=True
)
return Response(serializer.error, status=status.HTTP_400_BAD_REQUEST)`
Beta Was this translation helpful? Give feedback.
All reactions