python - Django REST 框架 : read-write Custom Relational Fields

标签 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'

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

最佳答案

你非常接近,from_native 方法的 data arg 是单个字符串值(它表示来自每个 recipients 键的值来自 payload),所以基于此,您需要检索相关对象,如果不存在则创建一个:

class SurveyRecipientField(serializers.RelatedField):
    ...

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

关于python - Django REST 框架 : read-write Custom Relational Fields,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24464510/

相关文章:

python - Python 2.5.2 的 lower() 和 upper() 是否遵循 Unicode?

python - xarray的interp nearest方法如何选择最近的中心?

python - 将 Pandas Csv 中的字符串转换为 Int 列

python - Django 在 AWS 上安装失败

api - 取回不记名 token postman flutter 腾

node.js - 使用 Postman 测试具有参数的 post API 调用

python - 使用列表作为列表框变量而不是元组

python - 我应该如何将这个又长又复杂的 PHP 样式 URL 查询字符串转换为 Django url?

python - DecimalField 问题,django 模型的 max_digits

python - 从 GetPocket API 检索标签 (Python)