2016-11-03 24 views
0

中を:(1つの)必要な位置引数不足して保存します。私はモデルを作成する '要求' ジャンゴ

class Person(models.Model): 
    name = models.CharField(max_length=250) 
    slug = AutoSlugField(populate_from='name') 
    birth_date = models.DateField(null=True, blank=True) 
    blood_group = models.CharField(max_length=5) 
    present_address = models.CharField(max_length=250, blank=True) 
    permanent_address = models.CharField(max_length=250, blank=True) 
    user = models.OneToOneField(
       settings.AUTH_USER_MODEL, 
       related_name='member_persons') 

forms.py:それは、最初の人が作成したためにうまく働い

class MemberForm(ModelForm): 

    class Meta: 
     model = Person 
     exclude = ('user',) 

    def save(self, request, commit=True): 
     person = super().save(commit=False) 
     if not person.pk: 
      person.user = get_user(request) 
     if commit: 
      person.save() 
      self.save_m2m() 
     return person 

。同じ人が別のデータを使ってフォームを作成しようとすると、 'save()'に1つの必須の位置引数 'request'があります。完全なトレースバック:

Traceback: 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner 
    39.    response = get_response(request) 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response 
    187.     response = self.process_exception_by_middleware(e, request) 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response 
    185.     response = wrapped_callback(request, *callback_args, **callback_kwargs) 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/base.py" in view 
    68.    return self.dispatch(request, *args, **kwargs) 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapper 
    67.    return bound_func(*args, **kwargs) 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view 
    23.     return view_func(request, *args, **kwargs) 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/utils/decorators.py" in bound_func 
    63.     return func.__get__(self, type(self))(*args2, **kwargs2) 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapper 
    67.    return bound_func(*args, **kwargs) 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view 
    23.     return view_func(request, *args, **kwargs) 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/utils/decorators.py" in bound_func 
    63.     return func.__get__(self, type(self))(*args2, **kwargs2) 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/base.py" in dispatch 
    88.   return handler(request, *args, **kwargs) 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/edit.py" in post 
    217.   return super(BaseCreateView, self).post(request, *args, **kwargs) 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/edit.py" in post 
    183.    return self.form_valid(form) 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/edit.py" in form_valid 
    162.   self.object = form.save() 

Exception Type: TypeError at /person/create/ 
Exception Value: save() missing 1 required positional argument: 'request' 

私のviews.py:

私はこのエラーを取得するのではなく、「あなたはすでにデータを提出した」というメッセージを表示したい
@require_authenticated_permission(
'member.add_person') 
class PersonCreate(CreateView): 
    template_name = 'member/person_form.html' 
    model = Person 
    success_url = '/person/' 
    form_class = MemberForm 

。このメッセージを人物作成フォームにどのように表示できますか? 助けていただければ幸いです。

編集:

新しいエラー:

トレースバック:

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner 
    39.    response = get_response(request) 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response 
    187.     response = self.process_exception_by_middleware(e, request) 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response 
    185.     response = wrapped_callback(request, *callback_args, **callback_kwargs) 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/base.py" in view 
    68.    return self.dispatch(request, *args, **kwargs) 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapper 
    67.    return bound_func(*args, **kwargs) 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view 
    23.     return view_func(request, *args, **kwargs) 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/utils/decorators.py" in bound_func 
    63.     return func.__get__(self, type(self))(*args2, **kwargs2) 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapper 
    67.    return bound_func(*args, **kwargs) 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view 
    23.     return view_func(request, *args, **kwargs) 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/utils/decorators.py" in bound_func 
    63.     return func.__get__(self, type(self))(*args2, **kwargs2) 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/base.py" in dispatch 
    88.   return handler(request, *args, **kwargs) 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/edit.py" in get 
    213.   return super(BaseCreateView, self).get(request, *args, **kwargs) 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/edit.py" in get 
    174.   return self.render_to_response(self.get_context_data()) 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/edit.py" in get_context_data 
    93.    kwargs['form'] = self.get_form() 

File "/home/ohid/test_venv/lib/python3.5/site-packages/django/views/generic/edit.py" in get_form 
    45.   return form_class(**self.get_form_kwargs()) 

Exception Type: TypeError at /person/create/ 
Exception Value: __init__() got an unexpected keyword argument 'request' 

答えて

4

save signatrueが無効です。引数にはrequestはありません。あなたはget_form

@require_authenticated_permission('member.add_person') 
class PersonCreate(CreateView): 
    template_name = 'member/person_form.html' 
    model = Person 
    success_url = '/person/' 
    form_class = MemberForm 

    def get_form(self, form_class=None): 
     form = super().get_form(form_class) 
     form.request = self.request 
     return form 

使用を形成し、その後、ご返信用

class MemberForm(ModelForm): 

    class Meta: 
     model = Person 
     exclude = ('user',) 

    def save(self, commit=True): 
     person = super().save(commit=False) 
     if not person.pk: 
      person.user = get_user(self.request) 
     if commit: 
      person.save() 
      self.save_m2m() 
     return person 
+0

感謝するためにそれを渡したい場合。新しいエラー__init __()に予期しないキーワード引数 'request'があります。私は主な質問に完全なトレースバックを追加しました。 –

+0

@djproでは代わりに 'get_form'を使います。更新された回答をご覧ください。私がログインしているユーザーと同じからのデータを提出しようとしたとき: –

+0

それはmember_person.user_id IntegrityErrorは/ UNIQUE制約が失敗した作成/人/で "提供します。 –

関連する問題