Я могу сохранить пользователя в таблице User, однако таблица UserModelInfo не обновляется.
ссылка на проект github - https://github.com/souradip-code/Django_myproject_1
Итак, что я в основном хочу, так это отношения OneToOne со django встроенным классом User с классом UserInfo и если я добавлю одного пользователя, обе таблицы должны обновиться, и каждый пользователь из таблицы UserInfo должен указать на пользователя в таблице User.
Вот что я сделал до сих пор -
Модели .py
JOB_CHOICES = (
(1, "Designer"),
(2, "Manager"),
(3, "Accounting")
)
CONTACT_CHOICES = (
(1, "+91"),
(2, "+92"),
(3, "+93"),
)
class UserInfoModel(models.Model):
user = models.OneToOneField(User,on_delete=models.CASCADE)
fullname = models.CharField(max_length=264,unique=True)
email = models.EmailField(max_length=255)
contact_pref = models.IntegerField(choices=CONTACT_CHOICES, default=1)
contact_suff =models.IntegerField()
job= models.IntegerField(choices=JOB_CHOICES, default=1)
pwd1 = models.CharField(max_length=20)
pwd2 = models.CharField(max_length=20)
def __str__(self):
return user.fullname
enter code here
Forms.py
JOB_CHOICES = (
(1, "Designer"),
(2, "Manager"),
(3, "Accounting")
)
CONTACT_CHOICES = (
(1, "+91"),
(2, "+92"),
(3, "+93"),
)
class RegisterForm(forms.ModelForm):
fullname = forms.CharField(max_length=264,widget=forms.TextInput(attrs={'class':'form-control'}))
email = forms.EmailField(max_length=255,widget=forms.EmailInput(attrs={'class':'form-control'}))
contact_pref = forms.ChoiceField(choices=CONTACT_CHOICES,widget=forms.Select(attrs={'class':'custom-select'}))
contact_suff =forms.IntegerField(widget=forms.NumberInput(attrs={'class':'form-control'}))
job= forms.ChoiceField(choices=JOB_CHOICES,widget=forms.Select(attrs={'class':'custom-select'}))
pwd1 = forms.CharField(max_length=20,widget=forms.PasswordInput(attrs={'class':'form-control'}))
pwd2 = forms.CharField(max_length=20,widget=forms.PasswordInput(attrs={'class':'form-control'}))
class Meta:
model = UserInfoModel
# fields = "__all__"
exclude =("user",)
Здесь, пожалуйста, проверьте конфигурацию метакласса
Просмотров. py
def user_register(request):
template = 'basic_app/register.html'
if request.method == 'POST':
form = RegisterForm(request.POST)
if form.is_valid():
if User.objects.filter(username=form.cleaned_data['fullname']).exists():
return render(request, template, {
'form': form,
'error_message': 'Username already exists.'
})
elif User.objects.filter(email=form.cleaned_data['email']).exists():
return render(request, template, {
'form': form,
'error_message': 'Email already exists.'
})
elif form.cleaned_data['pwd1'] != form.cleaned_data['pwd2']:
return render(request, template, {
'form': form,
'error_message': 'Passwords do not match.'
})
else:
# Create the user:
user = User.objects.create_user(
form.cleaned_data['fullname'],
form.cleaned_data['email'],
form.cleaned_data['pwd1']
)
user.contact_pref = form.cleaned_data['contact_pref']
user.contact_suff = form.cleaned_data['contact_suff']
user.job = form.cleaned_data['job']
user.save()
return render(request, 'basic_app/index.html')
return render(request, template,{'form': form})
else:
form = RegisterForm()
return render(request,template,{'form': form})
Регистрация. html
<form action="{% url 'basic_app:register' %}" method="post">
{% csrf_token %}
<div class="form-group input-group">
<div class="input-group-prepend">
<span class="input-group-text"> <i class="fa fa-user"></i> </span>
</div>
<!-- <input
name="fullname"
class="form-control"
placeholder="Full name"
type="text"
/> -->
{{ form.fullname }}
</div>
<!-- form-group// -->
<div class="form-group input-group">
<div class="input-group-prepend">
<span class="input-group-text">
<i class="fa fa-envelope"></i>
</span>
***********************rest of the code************************