Django REST框架:读写自定义关系字段

时间:2014-06-28 07:07:49

标签: python django api rest django-rest-framework

在DRF文档it says中,您可以将关系字段定义为可读写。但是,文档没有举例说明这一点,而且我遇到了错误。

以下是相关模型:

class Survey(models.Model):
    name = models.CharField(max_length=128)
    subjects = models.ManyToManyField(Subject, blank=True, null=True)
    ...

class Subject(models.Model):
    number = models.CharField(max_length=24)
    ...

这是序列化程序:

class SurveyRecipientField(serializers.RelatedField):
    many_widget = forms.TextInput()

    def to_native(self, value):
        return value.number

    def from_native(self, data):
        return Subject.objects.filter(number__in=data)


class SurveySerializer(serializers.HyperlinkedModelSerializer):
    ...
    recipients = SurveyRecipientField(source='subjects', many=True, read_only=False)
    ...

    class Meta:
        model = Survey
        fields = ('url', 'recipients', ...)
        lookup_field= 'pk'

我使用标准的ModelViewSet进行调查,没有覆盖任何内容。当我尝试创建一个调查对象:

{
    ...
    "recipients": ['8880008888', '9990009090']
    ...
}

我明白了:

Environment:


Request Method: POST
Request URL: http://localhost:8000/AO/2/api/survey/

Django Version: 1.6
Python Version: 2.7.1
Installed Applications:
('longerusername',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.sites',
 ...
)

Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  139.                 response = response.render()
File "/Library/Python/2.7/site-packages/django/template/response.py" in render
  105.             self.content = self.rendered_content
File "/Library/Python/2.7/site-packages/rest_framework/response.py" in rendered_content
  59.         ret = renderer.render(self.data, media_type, context)
File "/Library/Python/2.7/site-packages/rest_framework/renderers.py" in render
  577.         context = self.get_context(data, accepted_media_type, renderer_context)
File "/Library/Python/2.7/site-packages/rest_framework/renderers.py" in get_context
  554.             'post_form': self.get_rendered_html_form(view, 'POST', request),
File "/Library/Python/2.7/site-packages/rest_framework/renderers.py" in get_rendered_html_form
  443.             data = serializer.data
File "/Library/Python/2.7/site-packages/rest_framework/serializers.py" in data
  537.                 self._data = self.to_native(obj)
File "/Library/Python/2.7/site-packages/rest_framework/serializers.py" in to_native
  325.             value = field.field_to_native(obj, field_name)
File "/Library/Python/2.7/site-packages/rest_framework/relations.py" in field_to_native
  139.                 value = get_component(value, component)
File "/Library/Python/2.7/site-packages/rest_framework/fields.py" in get_component
  56.         val = getattr(obj, attr_name)
File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py" in __get__
  815.             through=self.field.rel.through,
File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py" in __init__
  512.                                  (instance, source_field_name))

Exception Type: ValueError at /AO/2/api/survey/
Exception Value: "<Survey: None>" needs to have a value for field "survey" before this many-to-many relationship can be used.

当我尝试PUT时,我得到:

Environment:


Request Method: POST
Request URL: http://localhost:8000/AO/2/api/survey/41289/

Django Version: 1.6
Python Version: 2.7.1
Installed Applications:
('longerusername',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.sites',
 ...)
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/rest_framework/viewsets.py" in view
  78.             return self.dispatch(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  57.         return view_func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/rest_framework/views.py" in dispatch
  399.             response = self.handle_exception(exc)
File "/Library/Python/2.7/site-packages/rest_framework/views.py" in dispatch
  396.             response = handler(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/rest_framework/mixins.py" in update
  137.             self.object = serializer.save(**save_kwargs)
File "/Library/Python/2.7/site-packages/rest_framework/serializers.py" in save
  560.             self.save_object(self.object, **kwargs)
File "/Library/Python/2.7/site-packages/rest_framework/serializers.py" in save_object
  942.                 setattr(obj, accessor_name, object_list)
File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py" in __set__
  830.         manager.add(*value)
File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py" in add
  571.                 self._add_items(self.source_field_name, self.target_field_name, *objs)
File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py" in _add_items
  644.                     '%s__in' % target_field_name: new_ids,
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in filter
  590.         return self._filter_or_exclude(False, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in _filter_or_exclude
  608.             clone.query.add_q(Q(*args, **kwargs))
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in add_q
  1198.         clause = self._add_q(where_part, used_aliases)
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in _add_q
  1232.                     current_negated=current_negated)
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in build_filter
  1122.                                                      lookup_type, value)
File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py" in get_lookup_constraint
  1100.                 (Constraint(alias, targets[0].column, sources[0]), lookup_type, value), AND)
File "/Library/Python/2.7/site-packages/django/utils/tree.py" in add
  104.         data = self._prepare_data(data)
File "/Library/Python/2.7/site-packages/django/db/models/sql/where.py" in _prepare_data
  79.             value = obj.prepare(lookup_type, value)
File "/Library/Python/2.7/site-packages/django/db/models/sql/where.py" in prepare
  352.             return self.field.get_prep_lookup(lookup_type, value)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_lookup
  371.             return [self.get_prep_value(v) for v in value]
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_value
  613.         return int(value)

Exception Type: TypeError at /AO/2/api/survey/41289/
Exception Value: int() argument must be a string or a number, not 'QuerySet'

我不了解如何序列化,反序列化和保存值列表。对于例如我不确定如何在from_native中正确实施SurveyRecipientField。我希望它是一个单独的数字,但它是一个数字列表。但是返回一个对象列表似乎并不可接受。

1 个答案:

答案 0 :(得分:1)

你非常接近,data arg for from_native方法是单个字符串值(它代表来自有效负载的每个recipients密钥的值),所以基于,您需要检索相关对象或创建一个相关对象:

class SurveyRecipientField(serializers.RelatedField):
    ...

    def from_native(self, data):
        return Subject.objects.get_or_create(number=data)