Tag Archives: rest

[django] add new field on serializer which is not defined on underlying model instance [SOLVED]

To add a field which will be computed on Serializer itself without flowing through underlying model instance, declare it as follows: class DbconfigExtendedSerializer(serializers.HyperlinkedModelSerializer): password_length = serializers.SerializerMethodField(‘getPasswordLength’) def getPasswordLength(self, instance): return len(instance.password) Simply declare your field of type serializers.SerializerMethodField(‘<method_name>’) and corresponding method will be called. sources http://stackoverflow.com/questions/18396547/django-rest-framework-adding-additional-field-to-modelserializer

[django] Could not resolve URL for hyperlinked relationship using view name “-detail” [SOLVED]

When trying to build my first rest-django app, i faced below error message, which was not easy to solve: Could not resolve URL for hyperlinked relationship using view name: “<model>-detail”. You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field. For simplicity reason, below example is based on django-rest-framework tutorial, where project is named “tutorial” and one app called “snippets” is created inside this project After investigation, the following happens: in snippets/urls.py, ensure to use “name=” attribute as per below, and use the convention of ‘<model>-detail’ for detail view […]