0
I am starting the studies in DRF and during a test I came across the following situation: it would be possible to change the fields
of a class with the exception of primary key
?
My first attempt was to allow the pk to be upgraded so that the other fields would be upgraded, but I was unsuccessful.
I understand that updating pk is not a good practice, but it also does not allow me to update the other class fields. I believe the mistake is silly, but I don’t know how to solve it.
Here’s what’s been done so far:
py.models.
class Pessoa(models.Model):
cpf = models.CharField(max_length=100, primary_key=True)
nome = models.CharField(max_length=100, default='')
sobrenome = models.CharField(max_length=100, default='')
cidade = models.CharField(max_length=100, default='')
estado = models.CharField(max_length=100, default='')
history = HistoricalRecords()
py serializers.
class VehicleSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Vehicle
fields = ['cpf','nome','sobrenome','cidade','estado']
def create(self, validated_data):
indent, created = Pessoa.objects.update_or_create(
cpf = validated_data.get('cpf', None),
defaults={'ident': validated_data.get('ident', None)})
return ident
You have here quite complete documentation, https://www.django-rest-framework.org/tutorial/1-serialization/
– Ernesto Casanova
As for your question update your pk to update the data, it does not exist and neither should you do. Update remaining attributes, yes you can do this.
– Ernesto Casanova