Mutually Referential Serializers Django Rest

Viewed 103

So I've been creating a bidding system in which each bid model is attached to a particular request instance or offer. Conversely, every request instance has a bidpick field representing the selected bid. It is easy to see that the code will exhibit what I like to call mutually referential design, the Bid class will refer to the Request class and vice versa.

Now I've moved on from standard Django to working with the Django Rest Framework, and my first step has been to serialize these models. Unfortunately, Python has no standard support for forward declarations nor does DRF support mutually referential classes right out of the box, so I've had to use a patch to handle recursive definitions like so:

class RequestSerial(ModelSerializer):

    servuser = UserSerial()

    bidpick = RecursiveField('BidSerial', allow_null = True, required = False)


    class Meta:

        model = Request

        fields = '__all__'


class BidSerial(ModelSerializer):

    biduser = UserSerial()

    request = RequestSerial()


    class Meta:

        model = Bid

        fields = '__all__'

Unfortunately, this doesn't really seem to work despite the fact that my code mirrors an example use case in the patch. I'm getting a max recursion depth exceeded error, and I was wondering if you guys had any thoughts or suggestions as to how I can create my serializers while avoiding this issue.

  File "C:\Python27\lib\site-packages\rest_framework\serializers.py", line 501, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "C:\Python27\lib\site-packages\rest_framework\serializers.py", line 501, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "C:\Python27\lib\site-packages\rest_framework\serializers.py", line 501, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "C:\Python27\lib\site-packages\rest_framework\serializers.py", line 501, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "C:\Python27\lib\site-packages\rest_framework\serializers.py", line 501, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "C:\Python27\lib\site-packages\rest_framework\serializers.py", line 501, in to_representation
    ret[field.field_name] = field.to_representation(attribute)
  File "C:\Python27\lib\site-packages\rest_framework\serializers.py", line 484, in to_representation
    fields = self._readable_fields
  File "C:\Python27\lib\site-packages\django\utils\functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Python27\lib\site-packages\rest_framework\serializers.py", line 378, in _readable_fields
    field for field in self.fields.values()
  File "C:\Python27\lib\site-packages\rest_framework\serializers.py", line 364, in fields
    for key, value in self.get_fields().items():
  File "C:\Python27\lib\site-packages\rest_framework\serializers.py", line 1017, in get_fields
    source, info, model, depth
  File "C:\Python27\lib\site-packages\rest_framework\serializers.py", line 1136, in build_field
    return self.build_standard_field(field_name, model_field)
  File "C:\Python27\lib\site-packages\rest_framework\serializers.py", line 1160, in build_standard_field
    field_kwargs = get_field_kwargs(field_name, model_field)
  File "C:\Python27\lib\site-packages\rest_framework\utils\field_mapping.py", line 77, in get_field_kwargs
    if model_field.verbose_name and needs_label(model_field, field_name):
  File "C:\Python27\lib\site-packages\rest_framework\utils\field_mapping.py", line 52, in needs_label
    return capfirst(model_field.verbose_name) != default_label
  File "C:\Python27\lib\site-packages\django\utils\functional.py", line 143, in __ne__
    return self.__cast() != other
  File "C:\Python27\lib\site-packages\django\utils\functional.py", line 131, in __cast
    return self.__text_cast()
  File "C:\Python27\lib\site-packages\django\utils\functional.py", line 119, in __text_cast
    return func(*self.__args, **self.__kw)
  File "C:\Python27\lib\site-packages\django\utils\text.py", line 26, in capfirst
    return x and force_text(x)[0].upper() + force_text(x)[1:]
  File "C:\Python27\lib\site-packages\django\utils\encoding.py", line 78, in force_text
    s = six.text_type(s)
  File "C:\Python27\lib\site-packages\django\utils\functional.py", line 119, in __text_cast
    return func(*self.__args, **self.__kw)
  File "C:\Python27\lib\site-packages\django\utils\translation\__init__.py", line 89, in ugettext
    return _trans.ugettext(message)
  File "C:\Python27\lib\site-packages\django\utils\translation\trans_real.py", line 345, in ugettext
    return do_translate(message, 'ugettext')
  File "C:\Python27\lib\site-packages\django\utils\translation\trans_real.py", line 324, in do_translate
    result = getattr(translation_object, translation_function)(eol_message)
  File "C:\Python27\lib\gettext.py", line 402, in ugettext
    missing = object()
RuntimeError: maximum recursion depth exceeded while calling a Python object
0 Answers
Related