0
models.py:はTypeError:__init __()が予期しないキーワード引数 'インスタンス' を得た
from django.db import models
from django.contrib.auth.models import User as BaseUser
CHOICE_GENDER = ((1, 'Male'), (2, 'Female'))
class Location(models.Model):
city = models.CharField(max_length=75)
country = models.CharField(max_length=25)
def __unicode__(self):
return ', '.join([self.city, self.state])
class Users(BaseUser):
user = models.OneToOneField(BaseUser, on_delete=models.CASCADE)
gender = models.IntegerField(choices=CHOICE_GENDER)
birth = models.DateField()
location = models.ForeignKey(Location)
class Meta:
ordering = ('user',)
forms.py:
from django.contrib.auth.forms import UserCreationForm
from django import forms
from .models import Users, Location, CHOICE_GENDER
class LocationForm(forms.ModelForm):
city = forms.CharField(max_length=75)
country = forms.CharField(max_length=25)
class Meta:
model = Location
fields = ('city', 'country',)
class RegistrationForm(UserCreationForm):
email = forms.CharField(max_length=75)
first_name = forms.CharField(max_length=30)
last_name = forms.CharField(max_length=30)
gender = forms.ChoiceField(choices=CHOICE_GENDER)
birth = forms.DateField()
location = LocationForm()
class Meta:
model = Users
fields = ('username', 'email', 'first_name', 'last_name', 'gender', 'birth', 'location')
def save(self, commit=True):
user = super(RegistrationForm, self).save(commit=False)
user.email = self.cleaned_data['email']
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.gender = self.cleaned_data['gender']
user.birth = self.cleaned_data['birth']
user.location = self.cleaned_data['location']
if commit:
user.save()
return user
views.py:
from django.shortcuts import render, redirect
from django.forms import formset_factory
from . import forms
def signup(request):
if request.method == "POST":
loc_form = forms.LocationForm(request.POST)
if loc_form.is_valid():
loc = loc_form.save()
RegistrationFormSet = formset_factory(forms.RegistrationForm)
formset = RegistrationFormSet(request.POST, instance=loc)
if formset.is_valid():
formset.save()
return redirect('./')
# else:
# how to make to render the POST data to the template page and fill all fields?
return render(request, 'signup.html')
Iタイトルからのエラーがformset = RegistrationFormSet(request.POST, instance=loc)
に届いています。私は主なものに保存する前に、別のフォームへの呼び出しを呼び出す有効なビューを作成しようとしているので、私はFormSetを使用しているのですが、それを動作させることはできません。誰かがそれについて私を助けることができますか?前もって感謝します!
パラメータ "model"が指定されている必要があります。私はその行を 'RegistrationFormSet = inlineformset_factory(forms.RegistrationForm、model = Users)'に変更しようとしましたが、うまくいかず、このエラーがスローされます: '' ModelFormOptions 'オブジェクトには' get_parent_list '属性がありません。助けてくれてありがとう! – MikiSoft