我試圖用Django Rest框架做一件事。 我有一個類通過外鍵鏈接到另一個類。 現在,我想創建一個API調用創建這兩個模型。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()
....
這也很好(我已經設置了meta等)。如果我去的API,我看到表格B加一的所有字段的URL把文本A.
然後我寫了這個觀點
class BCreate(CreateAPIView):
serializer_class = SerializerForB
def pre_save(self, obj):
b = B(text=self.request.DATA['text'])
b.save()
obj.B=b
的萬阿英,蔣達清與實際工作中節省的,並且數據具有場text
事實上,它說
'text' is an invalid keyword argument for this function
我應該寫一個API創建並使用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
在這個問題中值得包含異常的回溯。 –
有回溯,而不是獎勵數量是例子的文本。你可以看到問題是一樣的。 – EsseTi
您的問題是'reward_quantity'不是模型上的字段。 –