NP
2018-08-07 15:29:51 UTC
Hello,
I have following 2 models:
class State(models.Model):
name = models.CharField(blank=False, null=False, max_length=100)
description = models.TextField(null=True)
class Meta:
db_table = 'state'
def __str__(self):
return self.name
class Suite(models.model):
user = models.CharField(blank=False, null=False, max_length=100)
state = models.ForeignKey(State, related_name='state', on_delete=models.
PROTECT)
class Meta:
db_table = 'suite'
This one works:
~ curl -d '{"user":"nsupe","state":1}' -H "Content-Type: application/json" -XPUT
http://127.0.0.1:8000/api/suites/3/
But I would like to send the payload as follows:
~ curl -d '{"user":"nsupe","state":{"id":1,"name":"Quar","description":"state
desc"}}' -H "Content-Type: application/json" -XPUT http:
//127.0.0.1:8000/api/suites/3/
If no changes in `state` it should ignore it or if changes in state, it
should get updated along with the changes in suite record.
I tried
class SuitesSerializer(serializers.ModelSerializer):
state = QuarantinedStatesSerializer()
class Meta:
model = QuarantinedSuites
fields = ('id', 'user', 'state')
def update(self, instance, validated_data):
state = validated_data.pop('state')
print (state)
instance.state_id = state['id']
return instance
or
def update(self, instance, validated_data):
state = validated_data.pop('state')
print (state)
instance.description = state['description']
instance.name = state['name']
return instance
First update fails that there is no state id. state here don't have id even
though I am passing it.
For second it is gives name already exists and fails to update
Am I missing something here?
I have following 2 models:
class State(models.Model):
name = models.CharField(blank=False, null=False, max_length=100)
description = models.TextField(null=True)
class Meta:
db_table = 'state'
def __str__(self):
return self.name
class Suite(models.model):
user = models.CharField(blank=False, null=False, max_length=100)
state = models.ForeignKey(State, related_name='state', on_delete=models.
PROTECT)
class Meta:
db_table = 'suite'
This one works:
~ curl -d '{"user":"nsupe","state":1}' -H "Content-Type: application/json" -XPUT
http://127.0.0.1:8000/api/suites/3/
But I would like to send the payload as follows:
~ curl -d '{"user":"nsupe","state":{"id":1,"name":"Quar","description":"state
desc"}}' -H "Content-Type: application/json" -XPUT http:
//127.0.0.1:8000/api/suites/3/
If no changes in `state` it should ignore it or if changes in state, it
should get updated along with the changes in suite record.
I tried
class SuitesSerializer(serializers.ModelSerializer):
state = QuarantinedStatesSerializer()
class Meta:
model = QuarantinedSuites
fields = ('id', 'user', 'state')
def update(self, instance, validated_data):
state = validated_data.pop('state')
print (state)
instance.state_id = state['id']
return instance
or
def update(self, instance, validated_data):
state = validated_data.pop('state')
print (state)
instance.description = state['description']
instance.name = state['name']
return instance
First update fails that there is no state id. state here don't have id even
though I am passing it.
For second it is gives name already exists and fails to update
Am I missing something here?
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.