Good morning. I am building an API for users, I use jango REST Framework JSON API, the problem is that JSON Api responds in the following way:
{
"data": {
"type": "UserViewSet",
"id": "3",
"attributes": {
"password": "b'$2b$12$KANG.sypvTisDtEoT66opu4FpWhm.fs4ahf4RXzmluxCDl3D3f52m'",
"username": "test9099",
"email": "[email protected]",
"name": "test2",
"last_name": "test2_test1",
...
},
"relationships": {
"country": {
"data": {
"type": "Country",
"id": "2"
}
},
"state": {
"data": {
"type": "State",
"id": "1"
}
},
"city": {
"data": {
"type": "City",
"id": "2"
}
}
}
}
And what I need is this:
{
"data": {
"type": "UserViewSet",
"id": "3",
"attributes": {
"password": "b'$2b$12$KANG.sypvTisDtEoT66opu4FpWhm.fs4ahf4RXzmluxCDl3D3f52m'",
"username": "test9099",
"email": "[email protected]",
"name": "test2",
"last_name": "test2_test1",
...
},
"relationships": {
"country": {
"data": {
"type": "Country",
"id": "2"
"name": "Guatemala"
}
},
"state": {
"data": {
"type": "State",
"id": "1"
"name": "Guatemala"
"code": "09001"
}
},
"city": {
"data": {
"type": "City",
"id": "2"
"name": "GUATEMALA"
}
}
}
}
I do not know if in "data" of the "relationships" you can add more data, the attributes of the whole relationship.
code:
settings.py add this:
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10,
'EXCEPTION_HANDLER': 'rest_framework_json_api.exceptions.exception_handler',
'DEFAULT_PAGINATION_CLASS':
'rest_framework_json_api.pagination.PageNumberPagination',
'DEFAULT_PARSER_CLASSES': (
'rest_framework_json_api.parsers.JSONParser',
'rest_framework.parsers.FormParser',
'rest_framework.parsers.MultiPartParser'
),
'DEFAULT_RENDERER_CLASSES': (
'rest_framework_json_api.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer'
),
'DEFAULT_METADATA_CLASS': 'rest_framework_json_api.metadata.JSONAPIMetadata',
}
models.py
class Country(models.Model):
name = models.CharField(max_length=45)
country_code = models.CharField(max_length=10)
class State(models.Model):
name = models.CharField(max_length=45)
country = models.ForeignKey(Country, on_delete=models.CASCADE)
class City(models.Model):
name = models.CharField(max_length=45)
state = models.ForeignKey(State, on_delete=models.CASCADE)
country = models.ForeignKey(Country, on_delete=models.CASCADE)
class User(models.Model):
username = models.CharField(max_length=12)
name = models.CharField(max_length=45)
last_name = models.CharField(max_length=45)
email = models.EmailField(max_length=45)
country = models.ForeignKey(Country, on_delete=models.CASCADE)
state = models.ForeignKey(State, on_delete=models.CASCADE)
city = models.ForeignKey(City, on_delete=models.CASCADE)
password = models.CharField(max_length=255, blank=True)
serialize.py
class CountrySerializer(serializers.ModelSerializer):
class Meta:
model = Country
fields = ('__all__')
class StateSerializer(serializers.ModelSerializer):
class Meta:
model = State
fields = ('__all__')
class CitySerializer(serializers.ModelSerializer):
class Meta:
model = City
fields = ('__all__')
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('__all__')
Thanks for the help.