'How to use Property in Django Rest Framework model?
I am newbie to Django Rest Framework. I am have problem with using Property in Django rest framework
My Model
class AwsConsoleAccess(TimeStampedModel):
_resources = CharField(max_length=4096)
@resources.setter
def resources(self, resources: List[str]):
self._resources = ','.join(resources)
@property
def resources(self) -> List[str]:
if self._resources:
return self._resources.split(',')
return []
My serializer
class AwsConsoleAccessSerializer(ModelSerializer):
class Meta:
model = AwsConsoleAccess
fields = '__all__'
My view
def post(self, request, *args, **kwargs):
serializer = AwsConsoleAccessSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
My curl request
http://localhost:8000/access/842b1012-53ef-4e98-9a20-484e681c3789
request body:
{
"resources": [
{
"id": "dev-atm-jr1-platformcanary-APPLICATION"
}
]
}
Response
{
"_resources": [
"This field is required."
]
}
status code - 400 bad request
If I rename the resources to _resources in request_body it does not accept the List[str] .
I wanted to set the List of string
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
