Я изучаю django и в одной из своих задач я хочу добавить 3 внешних ключа в форму регистрации django -all-auth, но у меня есть ошибка: ограничение NOT NULL не выполнено: advertising_profile.user_id пожалуйста, вы можете помочь мне и объяснить, что мне не удается.
Я пытаюсь сохранить эту информацию для регистрационной формы
==========views.py==================
def register(request):
form = CustomSignUpProfileModelForm()
if request.method == "POST":
form = CustomSignUpProfileModelForm(data=request.POST)
if form.is_valid():
#user = form['customSignUp'].save(commit=False)
profile = form.save(commit=False)
profile.user = request.user
profile.save()
#user = form.save()
if profile is not None:
do_login(request, user)
return redirect('welcome')
#form.fields['username'].help_text = None
#form.fields['password1'].help_text = None
#form.fields['password2'].help_text = None
return render(request,"account/signup.html",{'form':form})
==========end views.py======================
=========forms.py======================
from django import forms
from django.forms import ModelForm
from betterforms.multiform import MultiModelForm
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib.auth.models import User
from .models import Profile, Region, Commune, Nation
class CustomSignUp(UserCreationForm):
username = forms.EmailField(label="Correo electrónico")
class Meta:
model = User
fields = ["username","password1","password2"]
class CustomSignIn(AuthenticationForm):
username = forms.EmailField(label="Correo Electrónico")
class Meta:
model = User
fields = ["username","password"]
class Profile(forms.ModelForm):
class Meta:
model = Profile
fields = ["name","last_name","mother_last_name","phone","region","commune","nation"]
class Region(forms.ModelForm):
class Meta:
model = Region
fields = ["name"]
class Commune(forms.ModelForm):
class Meta:
model = Commune
fields = ["name"]
class Nation(forms.ModelForm):
class Meta:
model = Nation
fields = ["name"]
class CustomSignUpProfileModelForm(MultiModelForm):
form_classes = {
'customSignUp': CustomSignUp,
'profile':Profile,
}
=============end forms.py =====================
============== models.py =====================
from django.db import models
from django.contrib.auth.models import User
from phone_field import PhoneField
# Create your models here.
class Region(models.Model):
cprovin = models.CharField(max_length=4)
name = models.CharField(max_length=50,verbose_name="Región")
def __str__(self):
return self.name
class Commune(models.Model):
cpoblac = models.CharField(max_length=4)
name = models.CharField(max_length=50, verbose_name="Comuna")
region = models.ForeignKey(Region,on_delete=models.CASCADE, verbose_name="Elija una región",null=False, blank=False)
def __str__(self):
return self.name
class Nation(models.Model):
name = models.CharField(max_length=50, verbose_name="Nacionalidad", null=False, blank=False)
def __str__(self):
return self.name
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE,null=False, blank=False)
name = models.CharField(max_length=30, verbose_name="Nombre", null=False, blank=False)
last_name = models.CharField(max_length=30, verbose_name="Apellido Paterno", null=False, blank=False)
mother_last_name = models.CharField(max_length=30, verbose_name="Apellido Materno", null=False, blank=False)
nation = models.ForeignKey(Nation,on_delete=models.CASCADE, verbose_name="Elija su nacionalidad", null=False, blank=False)
region = models.ForeignKey(Region,on_delete=models.CASCADE,verbose_name="Elija una región", null=False, blank=False)
commune = models.ForeignKey(Commune,on_delete=models.CASCADE,verbose_name="Elija una comuna", null=False, blank=False)
phone = PhoneField(null=False, blank=False, unique=True, help_text="Número de contacto")
def __str__(self):
return self.name
============== end models.py =====================