Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

model field named url and django rest framework url

I started to use the great django-rest-framework days ago. I'm not able to solve this simple issue.

My model contains a models.URLField named url.

My serializers.py file:

class ModelSerializer(serializers.HyperlinkedModelSerializer):
    owner = serializers.Field(source='owner.username')    

    class Meta:
        model = Model
        fields = ('url', 'owner', 'title', 'abstract', 'category', 'position', 'param1')

Checking the API result the field 'url' is populated with model.URLField.

"results": [
        {
            "url": "http://www.web.com", 
            "owner": "me", 
            "title": "title of the stuff"
        }

Instead I would like to have

"results": [
        {
            "url": "http://localhost:8000/en/apiv1/maps/4/", 
            "url_in_model": "http://www.web.com", 
            "owner": "me", 
            "title": "Forest fire"
        }

How can I solve? Thanks

like image 319
Daviddd Avatar asked Aug 14 '13 12:08

Daviddd


People also ask

How do you get a foreign key field name instead of ID in Django REST framework?

If you want to show any field of foreign key instead of id then you can use StringRelatedField.

What is the difference between Viewset and ModelViewSet?

It is more rigid than a generic APIView but it takes away from you some boilerplate/manual config that you would have to repeat again and again in most cases. One step further is the ModelViewSet , which is an extension of the ViewSet for when you are working with Django models.

What is the difference between ModelSerializer and HyperlinkedModelSerializer?

The HyperlinkedModelSerializer class is similar to the ModelSerializer class except that it uses hyperlinks to represent relationships, rather than primary keys. By default the serializer will include a url field instead of a primary key field.

What is the difference between APIView and Viewset?

APIView allow us to define functions that match standard HTTP methods like GET, POST, PUT, PATCH, etc. Viewsets allow us to define functions that match to common API object actions like : LIST, CREATE, RETRIEVE, UPDATE, etc.


2 Answers

It may be considered poor form (I am by no means a pro programmer or rest_framework expert), but I believe you can add extra context to the serialized output:

http://django-rest-framework.org/api-guide/serializers.html#specifying-fields-explicitly


class AccountSerializer(serializers.ModelSerializer):
    url = serializers.CharField(source='get_absolute_url', read_only=True)
    groups = serializers.PrimaryKeyRelatedField(many=True)

    class Meta:
        model = Account

Extra fields can correspond to any property or callable on the model.


So in the above the field 'get_absolute_url' must be in the 'Account' model. In your case (I think) you could do this:

class ModelSerializer(serializers.HyperlinkedModelSerializer):
    owner = serializers.Field(source='owner.username')
    url_in_model = serializer.Field(source='url')    

    class Meta:
        model = Model
        fields = ('url', 'url_in_model', 'owner', 'title', 'abstract', 'category', 'position', 'param1')

Of course you would pick the field type that suits.

I haven't had the chance to test this, so there is the chance that using 'url' as your source causes an issue and you may want to name your model field something else - apologies if that is the case and I have wasted your time.

Hope I have helped.

like image 87
WSV Avatar answered Sep 22 '22 13:09

WSV


The accepted answer didn't work for me in DRF 3. I got my model's url data for both url and url_in_model. To get the correct url value from DRF, it looked like:

class AccountSerializer(serializers.ModelSerializer):
    url = serializers.HyperlinkedIdentityField(view_name='account-detail')
    url_in_model = serializer.URLField(source='url')  

    class Meta:
        model = Account

account-detail should be replaced with whatever view corresponds to a single Account.

like image 25
Matt Cooper Avatar answered Sep 21 '22 13:09

Matt Cooper