UserProfileモデルでユーザーモデルを拡張したいとします。このUserProfileモデルには、ForeignKeyフィールドが含まれています。フォームでは、このフォームフィールドを事前に設定するためにModelChoiceFieldを使用したいと考えています。ForeignKeyフィールドを使用してフォームデータを保存する
私がフォームを送信するたびに、私は任意の助けをいただければ幸いです
ValueError at /accounts/register/
Cannot assign "'13'": "UserProfile.course" must be a "Course" instance.
を入手!
マイコード:
models.py
class Course(models.Model):
course_accid = models.CharField(max_length=10)
def __str__(self):
return self.course_accid
class UserProfile(models.Model):
# This line is required. Links UserProfile to a User model instance.
user = models.OneToOneField(User)
website = models.URLField(blank=True)
picture = models.ImageField(upload_to='profile_images', blank=True)
course = models.ForeignKey(Course)
def __unicode__(self):
return self.user.username
def user_registered_callback(sender, user, request, **kwargs):
profile = UserProfile(user = user)
profile.website = request.POST["website"]
profile.course = Course.objects.get(pk=request.POST["course"]),
profile.save()
forms.py
class RegistrationForm(RegistrationForm):
course = forms.ModelChoiceField(queryset=Course.objects.all())
website = forms.URLField()
ユーザーを登録しようとしているビューも表示できますか?あなたはフォームの検証をどのように扱いますか?また、 'class RegistrationForm(RegistrationForm):'は正しく見えません。 – ozgur
私はそれを処理するためにdjango-registrationを使用するので、それを処理するためのビューメソッドまたはクラスはありません。 RegistrationForm(RegistrationForm)は私が今気づいていない不運な名前の選択かもしれません。私はフォーム・レンダラーのカスタムUserProfileフィールドを自分自身で追加してdjango登録からRegistrationFormを修正します。私は別のものを選ぶべきだった。 – kftb