Extending User Model Django, IntegrityError -
i trying extend user
model create profile
model. following code displays form additional fields specified location
, bio
. when submit form original username
, first_name
, last_name
, email
, , password
fields stored in database @ http://127.0.0.1:8000/admin, none of custom fields stored in profile section added admin . following error:
integrityerror @ /accounts/register/ not null constraint failed: accounts_profile.user_id request method: post request url: http://127.0.0.1:8000/accounts/register/ django version: 1.11.2 exception type: integrityerror exception value: not null constraint failed: accounts_profile.user_id exception location: /library/frameworks/python.framework/versions/3.6/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py in execute, line 328 python executable: /library/frameworks/python.framework/versions/3.6/bin/python3.6
models.py:
from django.db import models django.contrib.auth.models import user django.db.models.signals import post_save django.dispatch import receiver class profile(models.model): user = models.onetoonefield(user, on_delete=models.cascade) bio = models.textfield(max_length=500, blank=true) location = models.charfield(max_length=30, blank=true) birth_date = models.datefield(null=true, blank=true) @receiver(post_save, sender=user) def create_user_profile(sender, instance, created, **kwargs): if created: profile.objects.create(user=instance) @receiver(post_save, sender=user) def save_user_profile(sender, instance, **kwargs): instance.profile.save()
forms.py:
from django import forms django.contrib.auth.models import user django.contrib.auth.forms import usercreationform .models import profile class registrationform(usercreationform): email = forms.emailfield(required = true) class meta: model = user #model user comes username, email, first name, last name , pass1 , pass2 fields fields = ( 'username', 'email', 'first_name', 'last_name', 'password1', 'password2' ) def save(self, commit = true): user = super(registrationform, self).save(commit = false) user.first_name = self.cleaned_data['first_name'] user.last_name = self.cleaned_data['last_name'] user.email = self.cleaned_data['email'] if commit: user.save() return user class profileform(forms.modelform): class meta: model = profile fields = ('location','bio')
views.py:
from django.shortcuts import render, redirect .forms import registrationform,profileform def register(request): if request.method == 'post': form = registrationform(request.post) profile_form = profileform(request.post) if form.is_valid() , profile_form.is_valid(): form.save() profile_form.save() return redirect('login') else: form = registrationform() profile_form = profileform() return render(request, 'accounts/register.html', {'form': form, 'profile_form': profile_form})
urls.py:
from django.conf.urls import url django.contrib.auth.views import login . import views urlpatterns = [ # /accounts/ url(r'^$', views.index, name = 'accounts'), # /accounts/register/ url(r'^register/$', views.register, name='register'), url(r'^login/$', login, {'template_name': 'accounts/login.html'}, name='login'), ]
admin.py:
from django.contrib import admin .models import profile # register models here. admin.site.register(profile) # register models here.
any appreciated.
you have set relation manually view should this:
if form.is_valid() , profile_form.is_valid(): user_object = form.save() = profile_form.save(commit=false) a.user = user_object a.save() return redirect('login')
Comments
Post a Comment