相互参照序列化程序Django Rest

时间:2017-07-20 15:08:37

标签: python django serialization django-rest-framework

所以我一直在创建一个出价系统,其中每个出价模型都附加到特定的请求实例或要约。相反,每个请求实例都有一个代表所选出价的bidpick字段。很容易看出代码将展示我喜欢称之为相互参照设计的内容,Bid类将引用Request类,反之亦然。

现在我已经从标准Django转向使用Django Rest Framework了,我的第一步是序列化这些模型。遗憾的是,Python没有对前向声明的标准支持,DRF也不支持开箱即用的相互引用类,所以我不得不使用patch来处理递归定义,如下所示:

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__'

不幸的是,尽管我的代码反映了补丁中的示例用例,但这似乎并不真正起作用。我的最大递归深度超出错误,我想知道你们是否有任何想法或建议如何在避免这个问题的同时创建我的序列化器。

  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 个答案:

没有答案