Extending User model in Django -


i trying create profile model of users. have following code in 1 custom field birth_date, cannot seem of other custom fields work:

forms.py:

from django import forms django.contrib.auth.forms import usercreationform django.contrib.auth.models import user  class signupform(usercreationform):     birth_date = forms.datefield(help_text='required. format: yyyy-mm-dd')      class meta:         model = user         fields = ('username', 'first_name','last_name', 'birth_date','location', 'password1', 'password2', ) 

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() 

views.py

from django.contrib.auth import login, authenticate django.shortcuts import render, redirect django.http import httpresponse .forms import signupform  def home(request):     return httpresponse('fd')   def signup(request):     if request.method == 'post':         form = signupform(request.post)         if form.is_valid():             user = form.save()             user.refresh_from_db()  # load profile instance created signal             user.profile.birth_date = form.cleaned_data.get('birth_date')             user.profile.college = form.cleaned_data.get('college')             user.save()             raw_password = form.cleaned_data.get('password1')             user = authenticate(username=user.username, password=raw_password)             login(request, user)             return redirect('home')     else:         form = signupform()     return render(request, 'accounts/profile.html', {'form': form}) 

urls.py

urlpatterns = [      url(r'^register/$', views.signup, name='profile'),     url(r'^home/$', views.home, name='home'),  ] 

if remove locationfrom forms.py, form load , allow me fill out , upon submission data, including custom field birth_date save. when try add location form, following error:

file "/library/frameworks/python.framework/versions/3.6/lib/python3.6/site-packages/django/forms/models.py", line 262, in __new__     raise fielderror(message) django.core.exceptions.fielderror: unknown field(s) (location) specified user 

i not sure why cannot add location field form.

because it's not field on user. need same have birth_date - declare separately in form class, , save explicitly profile in view.


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -