Сбой ограничения NOT NULL: account_user profile.user_id - PullRequest
0 голосов
/ 28 апреля 2019

Привет, я пытаюсь сохранить информацию о профиле пользователя, но получаю ошибку, и я новый django

это мой form.py:

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import UserProfile

  class RegistrastionForm(UserCreationForm):
   email = forms.EmailField(required=True)
      class Meta:
    proxy = True
    model = User
    fields = (
    'username',
    'first_name',
    'last_name',
    'email',
    'password1',
    'password2',
    )
    help_texts = {
    'username' : 'Your username cannot contain spaces and must cannot exceed 10 characters. You can use letters, digits and @/./+/-/_ only.'
    }
    def save(self,commit=True):
        user = super(RegistrastionForm,self).save(commit=False)
        user.first_name = cleaned_data['first_name']
        user.last_name = cleaned_data['last_name']
        user.email = cleaned_data['email']
        if commit:
            user.save()
        return user

class UserProfileForm(forms.ModelForm):
class Meta:
    model = UserProfile
    fields = ('surname',
    'initial',
    'given',
    'title',
    'street',
    'suburb',
    'state',
    'postcode',
    'phone',
    'mobile',
    )
    def save(self,commit=True):
        user = super(RegistrstionForm,self).save(commit=False)
        user.surname = cleaned_data['surname']
        user.initial = cleaned_data['initial']
        user.given = cleaned_data['given']
        user.title = cleaned_data['title']
        user.street = cleaned_data['street']
        user.suburb = cleaned_data['subrub']
        user.state = cleaned_data['state']
        user.postcode = cleaned_data['postcode']
        user.phone = cleaned_data['phone']
        user.mobile = cleaned_data['mobile']
        if commit:
            user.save()

        return user

class EditProfileForm(UserChangeForm):
class Meta:
    model = UserProfile
    fields = ('surname',
    'initial',
    'given',
    'title',
    'street',
    'suburb',
    'state',
    'postcode',
    'phone',
    'mobile',
    'password'
    )
    help_texts = {
    'password' : 'This information cannot be changed. You will need to 
    contact support to have your password reset.'
    }
    exclude = ()

это моя модель:

  from django.db import models
  from django.contrib.auth.models import User

  class UserProfile(models.Model):
 user = models.OneToOneField(User,default=None, null=True, 
 on_delete=models.CASCADE)
 id=models.AutoField(primary_key=True)
 surname=models.CharField(max_length=50)
 initial=models.CharField(max_length=50)
 given=models.CharField(max_length=50)
 title=models.CharField(max_length=3)
 street = models.CharField(max_length=50)
 suburb = models.CharField(max_length=50)
 state = models.CharField(max_length=3)
 postcode = models.CharField(max_length=4)
 phone=models.CharField(max_length=8)
 mobile=models.CharField(max_length=10)
 TUTOR_ID=models.IntegerField(default=False)
 #istutor = models.BooleanField(default=False)

def __str__(self):
    return self.user.username

это мой view.py:

   from django.shortcuts import render, redirect
   from accounts.form import RegistrastionForm, UserProfileForm, 
     EditProfileForm
 from django.conf import settings
 from django.contrib.auth.decorators import login_required

from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from django.template.context_processors import csrf
from .models import UserProfile


# Create your views here.
#Function to provide the view at signup.html. It takes the POST request 
from 
 the HTML and checks the validity of the information and parses it if 
  true.
def signup_view(request):
 if request.method =='POST':
     form = RegistrastionForm(request.POST)
     if form.is_valid():
         form.save()
         return redirect('/accountRegistration/additionalSignup')
     else:
         print(form.errors)
 else:
      form = RegistrastionForm()
      context = {'form' : form }
      return render(request,'accounts/signup.html', context)

  #Function to provide the view at signup2.html. It takes the POST request 
  from the HTML and checks the validity of the information and parses it 
    if 
  true.
  def additionalSignup_view(request): #doesnt stop if the information is 
  incorrect
  if request.method =='POST':
     profile_form = UserProfileForm(request.POST)
     if profile_form.is_valid():
         profile_form.save()
         return redirect('/signInPage/signin/')
     else:
         print(profile_form.errors)
 else:
      profile_form = UserProfileForm()
      context = {'profile_form' : profile_form}
      return render(request,'accounts/signup2.html', context)


 #Function to provide the view for the edit information form. It takes the 
 POST request from the HTML and checks the validity of the information and 
 parses it if true and redirects back to the member page.
 def edit_profile(request):
  if request.method == 'POST':
     form = EditProfileForm(request.POST, instance=request.user)
     p_form = UserProfileForm(request.POST, instance = 
 request.user.userprofile)
     args = {}
     args.update(csrf(request))
     args['form']= form
     if form.is_valid() and p_form.is_valid():
         form.save()
         p_form.save()
     return redirect('/member/')
 else:
     form = EditProfileForm(instance=request.user.userprofile)
     args = {}
     args.update(csrf(request))
     args['form']= form
     return render(request, 'accounts/edit_profile.html', args)

У меня появляется эта ОШИБКА после отправки формы, которую я сделал, чтобы позволить пользователю заполнить информацию профиля сразу после подтверждения по электронной почте. Посмотрите на эту диаграмму

это моя ошибка https://i.imgur.com/jacony0.png

...