python - Control output of Django Rest Framework -



python - Control output of Django Rest Framework -

my rest api functioning correctly, output id numbers. how can 'role_type' display name instead of id number?

output:

{"count": 2, "next": null, "previous": null, "results": [{"user": {"username": "smithb", "first_name": "bob", "last_name": "smith"}, "role_type": 2, "item": 1}, {"user": {"username": "jjones", "first_name": "jane", "last_name": "jones"}, "role_type": 2, "item": 1}]}

serializers.py

class roleserializer(serializers.modelserializer): user = personshortserializer(many=false, read_only=true) class meta: model = role fields = 'user', 'role_type', 'item' def get_role_type(self, obj): homecoming obj.name

models.py

class role(models.model): role_type = models.foreignkey('roletype') user = models.foreignkey(person) item = models.foreignkey('assets.item') class roletype(models.model): name = models.charfield(max_length=255) permissions = models.manytomanyfield(rolepermission, blank=true, null=true) def __unicode__(self): homecoming self.name

take @ different types of serializer relationship fields.

in particular relatedfield should need it'll represent target of relationship using unicode value.

class roleserializer(serializers.modelserializer): user = personshortserializer(many=false, read_only=true) role_type = serializers.relatedfield() class meta: model = role fields = ('user', 'role_type', 'item')

also note relatedfield read field, there's no way determine appropriate model instance given unicode representation. if did need writable might want @ implementing custom relational field.

python django django-rest-framework

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -