在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
。我原以为是一个单独的数字,但它是一个数字列表。但返回对象列表似乎是不可接受的。 最佳答案
您非常接近,data
> ARG,用于from_native
方法是一个单独的字符串值(它代表来自“有效载荷”的每个recipients
键的值),因此基于此,您需要检索相关对象,或者如果不存在则创建一个对象:
class SurveyRecipientField(serializers.RelatedField):
...
def from_native(self, data):
return Subject.objects.get_or_create(number=data)
关于python - Django REST框架:读写自定义关系字段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24464510/