我正在尝试用 Django Rest Framework 做一件事。我有一个类通过外键链接到另一个类。现在,我想创建一个 API 调用来创建两个模型。
class A(models.Model):
text = models.CharField(...)
...
class B(models.Model):
...
myfield= models.ForeignKey(A)
没关系。
class SerializerForB(serializers.HyperlinkedModelSerializer):
text = serializers.CharField()
....
这也很好(我已经设置了元等)。如果我转到 API 的 URL,我会看到包含 B 的所有字段以及放置 A 文本的表单。
然后我写了这个视图
class BCreate(CreateAPIView):
serializer_class = SerializerForB
def pre_save(self, obj):
b = B(text=self.request.DATA['text'])
b.save()
obj.B=b
问题在于 A 的实际保存以及数据具有text
它所说的字段的事实
'text' is an invalid keyword argument for this function
我应该编写一个 API 来创建 A 并pk
在我通过 API 创建 B 时使用它吗?或者我可以同时做两个吗?
环境:
Request Method: POST
Request URL: http://localhost:8000/api/task/human/create/
Django Version: 1.5.1
Python Version: 2.7.2
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'django.contrib.messages',
'django.contrib.flatpages',
'django.contrib.markup',
'django.contrib.admin',
'general',
'requester',
'executor',
'crispy_forms',
'social_auth',
'developer',
'restapi',
'requests',
'rest_framework',
'rest_framework.authtoken',
'djrill',
'mturk',
'jsonify',
'gravatar')
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 "/Users/stefanotranquillini/.virtualenvs/CC/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/stefanotranquillini/.virtualenvs/CC/lib/python2.7/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/Users/stefanotranquillini/.virtualenvs/CC/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
77. return view_func(*args, **kwargs)
File "/Users/stefanotranquillini/.virtualenvs/CC/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
326. response = self.handle_exception(exc)
File "/Users/stefanotranquillini/.virtualenvs/CC/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
323. response = handler(request, *args, **kwargs)
File "/Users/stefanotranquillini/.virtualenvs/CC/lib/python2.7/site-packages/rest_framework/generics.py" in post
361. return self.create(request, *args, **kwargs)
File "/Users/stefanotranquillini/.virtualenvs/CC/lib/python2.7/site-packages/rest_framework/mixins.py" in create
50. if serializer.is_valid():
File "/Users/stefanotranquillini/.virtualenvs/CC/lib/python2.7/site-packages/rest_framework/serializers.py" in is_valid
478. return not self.errors
File "/Users/stefanotranquillini/.virtualenvs/CC/lib/python2.7/site-packages/rest_framework/serializers.py" in errors
470. ret = self.from_native(data, files)
File "/Users/stefanotranquillini/.virtualenvs/CC/lib/python2.7/site-packages/rest_framework/serializers.py" in from_native
866. instance = super(ModelSerializer, self).from_native(data, files)
File "/Users/stefanotranquillini/.virtualenvs/CC/lib/python2.7/site-packages/rest_framework/serializers.py" in from_native
323. return self.restore_object(attrs, instance=getattr(self, 'object', None))
File "/Users/stefanotranquillini/.virtualenvs/CC/lib/python2.7/site-packages/rest_framework/serializers.py" in restore_object
851. instance = self.opts.model(**attrs)
File "/Users/stefanotranquillini/.virtualenvs/CC/lib/python2.7/site-packages/django/db/models/base.py" in __init__
415. raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
Exception Type: TypeError at /api/task/human/create/
Exception Value: 'reward_quantity' is an invalid keyword argument for this function