title: How to serialize multiple relationship models in django rest framework
body: <p>I'm new to this Django rest framework and I'm trying to create an API. My model.py looks like the following</p>
  
  <pre><code>class FieldTypes(models.Model):
      field_type = models.CharField(max_length=255, null=False)
      deleted = models.BooleanField(default=False)
  
  class Forms(models.Model):
      form_name = models.CharField(max_length=255, null=False)
      deleted = models.BooleanField(default=False)
  
  class FormFields(models.Model):
      form = models.ForeignKey(Forms, related_name = 'form_fields', on_delete=models.CASCADE)
      field_type = models.ForeignKey(FieldTypes, related_name = 'form_field_types', on_delete=models.CASCADE)
      description = models.CharField(max_length=255, null=False)
      deleted = models.BooleanField(default=False)
  
  class FormFieldOptions(models.Model):
      form_field = models.ForeignKey(FormFields, related_name = 'form_field_options', on_delete=models.CASCADE)
      description = models.CharField(max_length=255, null=False)
      deleted = models.BooleanField(default=False)
  </code></pre>
  
  <p>and the serializers.py looks like </p>
  
  <pre><code>class FormFieldSerializer(serializers.ModelSerializer):
      form_field_options = FormFieldOptionsSerializer(many=True)
      class Meta:
          model = FormFields
          fields = ('id', 'description', 'form_field_options')
  
  class FormSerializer(serializers.ModelSerializer):
      form_fields = FormFieldSerializer(many=True)
      class Meta:
          model = Forms
          fields = ('form_name', 'form_fields')
  
  class FieldTypeSerializer(serializers.ModelSerializer):
      class Meta:
          model = FieldTypes
          fields = ('field_type')
  </code></pre>
  
  <p>with that i was able to produce a result a like </p>
  
  <pre><code>{
      "form_name": "Ticket",
      "form_fields": [
          {
              "id": 1,
              "description": "Type",
              "form_field_options": [
                  {
                      "id": 1,
                      "description": "Question"
                  },
                  {
                      "id": 2,
                      "description": "Incident"
                  },
                  {
                      "id": 3,
                      "description": "Problem"
                  },
                  {
                      "id": 4,
                      "description": "Task"
                  }
              ]
          }
      ]
  }
  </code></pre>
  
  <p>but i need to know which field type the form fields belongs to. And I'm not sure about how to bring field types in the JSON data. Kindly guide me to get the expected result. Please let me know if I Should reform the data models to get the expected result.</p>
  
  <p>Traceback:</p>
  
  <pre><code>File "C:\Python\lib\site-packages\django\core\handlers\exception.py" in inner
    34.             response = get_response(request)
  
  File "C:\Python\lib\site-packages\django\core\handlers\base.py" in _get_response
    126.                 response = self.process_exception_by_middleware(e, request)
  
  File "C:\Python\lib\site-packages\django\core\handlers\base.py" in _get_response
    124.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)
  
  File "C:\Python\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
    54.         return view_func(*args, **kwargs)
  
  File "C:\Python\lib\site-packages\rest_framework\viewsets.py" in view
    103.             return self.dispatch(request, *args, **kwargs)
  
  File "C:\Python\lib\site-packages\rest_framework\views.py" in dispatch
    483.             response = self.handle_exception(exc)
  
  File "C:\Python\lib\site-packages\rest_framework\views.py" in handle_exception
    443.             self.raise_uncaught_exception(exc)
  
  File "C:\Python\lib\site-packages\rest_framework\views.py" in dispatch
    480.             response = handler(request, *args, **kwargs)
  
  File "C:\Python\lib\site-packages\rest_framework\mixins.py" in list
    48.         return Response(serializer.data)
  
  File "C:\Python\lib\site-packages\rest_framework\serializers.py" in data
    765.         ret = super(ListSerializer, self).data
  
  File "C:\Python\lib\site-packages\rest_framework\serializers.py" in data
    262.                 self._data = self.to_representation(self.instance)
  
  File "C:\Python\lib\site-packages\rest_framework\serializers.py" in to_representation
    683.             self.child.to_representation(item) for item in iterable
  
  File "C:\Python\lib\site-packages\rest_framework\serializers.py" in &lt;listcomp&gt;
    683.             self.child.to_representation(item) for item in iterable
  
  File "C:\Python\lib\site-packages\rest_framework\serializers.py" in to_representation
    527.                 ret[field.field_name] = field.to_representation(attribute)
  
  File "C:\Python\lib\site-packages\rest_framework\serializers.py" in to_representation
    683.             self.child.to_representation(item) for item in iterable
  
  File "C:\Python\lib\site-packages\rest_framework\serializers.py" in &lt;listcomp&gt;
    683.             self.child.to_representation(item) for item in iterable
  
  File "C:\Python\lib\site-packages\rest_framework\serializers.py" in to_representation
    510.         fields = self._readable_fields
  
  File "C:\Python\lib\site-packages\django\utils\functional.py" in __get__
    37.         res = instance.__dict__[self.name] = self.func(instance)
  
  File "C:\Python\lib\site-packages\rest_framework\serializers.py" in _readable_fields
    376.             field for field in self.fields.values()
  
  File "C:\Python\lib\site-packages\rest_framework\serializers.py" in fields
    363.             for key, value in self.get_fields().items():
  
  File "C:\Python\lib\site-packages\rest_framework\serializers.py" in get_fields
    1045.                 source, info, model, depth
  
  File "C:\Python\lib\site-packages\rest_framework\serializers.py" in build_field
    1190.         return self.build_unknown_field(field_name, model_class)
  
  File "C:\Python\lib\site-packages\rest_framework\serializers.py" in build_unknown_field
    1302.             (field_name, model_class.__name__)
  
  Exception Type: ImproperlyConfigured at /forms/
  Exception Value: Field name `field_type` is not valid for model `FormFields`.
  </code></pre>