Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foreign key field disappears in swagger docs after adding depth attribute in Serializer

Whenever I define the depth attribute, the foreign key field from swagger docs in POST section disappears. That seems strange because I required depth = 1 when I want related data in my GET request. So I can not remove this in order to get this related field parameter in the POST section.

Here is the case.

Model:

from django.db import models
from django.conf import settings
# Create your models here.

User = settings.AUTH_USER_MODEL

class Todo(models.Model):
    user = models.ForeignKey(User)
    title = models.CharField("Title", max_length=255)
    completed = models.BooleanField("Completed")

Serializer without depth =1.

from rest_framework import serializers
from models import Todo

class TodoSerializer(serializers.HyperlinkedModelSerializer):
   class Meta:
       model = Todo

Swagger output: enter image description here

Now If I add depth = 1 than Swagger does not display related field. enter image description here

Let me know if anyone has any clue about this.

Thanks :)

like image 260
CrazyGeek Avatar asked Jan 02 '16 10:01

CrazyGeek


2 Answers

Finally after digging into this, I come up with solution by which we can avoid this issue and achieve the expected solution.

So the solution is "Instead of using depth = 1 attribute we can using related serializer instance it self where it works similar to depth functionality."

Here is tested solution

Model:

from django.db import models
from django.conf import settings

User = settings.AUTH_USER_MODEL

class Todo(models.Model):
    user = models.ForeignKey(User)
    title = models.CharField("Title", max_length=255)
    completed = models.BooleanField("Completed")

Serializer

from rest_framework import serializers
from django.conf import settings
from models import Todo

User = settings.AUTH_USER_MODEL


class UserSerializer(serializers.HyperlinkedSerializer):
    class Meta:
       model = User


class TodoSerializer(serializers.HyperlinkedModelSerializer):
   user = UserSerializer() 

   class Meta:
      model = Todo
      fields = ('user', 'title', 'completed')

Swagger Output: enter image description here

This solution is kind of different approach in order to achieve the required functionality, But still I am expecting an official solution from django-rest-swagger team, Even I have posted the same query on django-rest-swagger github repo here.

like image 148
CrazyGeek Avatar answered Oct 24 '22 20:10

CrazyGeek


One solution is to just don't use depth and override to_representation method of serializer:

class TodoSerializer(serializers.ModelSerializer):
    class Meta:
        model = Todo

    def to_representation(self, instance):
        r = super(TodoSerializer, self).to_representation(instance)
        r.update({'user': UserSerializer().to_representation(instance.user)})
        return r

This way, in post everything will be as it was, and in get when return json of todo then to_representation will be called and will add user to json data.

like image 42
Reza Torkaman Ahmadi Avatar answered Oct 24 '22 18:10

Reza Torkaman Ahmadi