Home:ALL Converter>django rest framework problems wiht reverse url

django rest framework problems wiht reverse url

Ask Time:2016-01-08T03:14:16         Author:AramirezMiori

Json Formatter

I'm having problems with Django Rest Framework reverse function (from rest_framework.reverse import reverse) and django_reverse.

When I try to return the url of a single object, I'm getting all the url pattern, instead of the ID I get the text representation of the model.

For example I have a model called Driver, when I try to get the url of a Driver instance with pk=1, the response is

http : //base_url/driver/driver%20object/.

This can be resolve by overriding Driver unicode function to return str(self.pk), but I don't like this approach.

Also previous versions of django/drf did not present this issue.

First of all my virtual-env is:

  • Django==1.8.4
  • django-braces==1.8.0
  • django-extensions==1.5.5
  • django-filter==0.10.0
  • django-oauth-toolkit==0.8.1
  • djangorestframework==3.3.2
  • psycopg2==2.6 etc

Models

    class Dominio(AuditableModel):
        dominio = models.CharField(max_length=200, blank=False, null=False, unique=True)
        pais = models.ForeignKey(Pais, null=False, blank=False)

        @property
        def owners(self):
            if self.conductores.count() > 0:
                return self.conductores.filter(is_owner=True).all()[0]
            else:
                return None


    class Conductor(AuditableModel):
        usuario = models.ForeignKey(User, null=False, blank=False, related_name='dominios')
        dominio = models.ForeignKey(Dominio, null=False, blank=False, related_name='conductores')
        is_owner = models.BooleanField(default=False, null=False)
        #
        # def __unicode__(self):
        #     return str(self.pk)

        class Meta:
            verbose_name_plural = "conductores"

Serializers

    class ConductorSerializer(serializers.HyperlinkedModelSerializer):
        class Meta:
            model = Conductor
            fields = ('id', 'url', 'usuario', 'dominio', 'is_owner')


    class DominioSerializer(serializers.HyperlinkedModelSerializer):
        owners = serializers.HyperlinkedRelatedField(read_only=True, view_name='conductor-detail')
        class Meta:
            model = Dominio
            fields = ('id', 'url', 'dominio', 'pais', 'owners')

result "owners": "http : //localhost:8000/ventana/api/v1/conductores/Conductor%20object/"

Django rest framework api response

but, if I change the @property owners method to return a list of drivers (conductores), then the urls are correct

Models

    class Dominio(AuditableModel):
        dominio = models.CharField(max_length=200, blank=False, null=False, unique=True)
        pais = models.ForeignKey(Pais, null=False, blank=False)

        @property
        def owners(self):
            if self.conductores.count() > 0:
                return self.conductores.filter(is_owner=True).all()
            else:
                return []

Serializers

    class DominioSerializer(serializers.HyperlinkedModelSerializer):
        owners = serializers.HyperlinkedRelatedField(many=True, read_only=True, view_name='conductor-detail')
        class Meta:
            model = Dominio
            fields = ('id', 'url', 'dominio', 'pais', 'owners')

Result "owners": [ "http : //localhost:8000/ventana/api/v1/conductores/1/"]

Django rest framework api response 2

Author:AramirezMiori,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/34663308/django-rest-framework-problems-wiht-reverse-url
yy