I would like to add new fields at user profile. Its working on admin side but it isnt saving on frontend…
Its very curious because no error is showing, it just doenst save.
In the following code I added the field “CPF” to userprofile. Its showed correctly in the frontend form and then I try to save it (some problem happen here)
I appreciate any help.
models.py
from django.db import models
from django.contrib.auth.models import User
...
cpf = models.CharField('CPF',max_length=14)
...
def __unicode__(self):
return self.cpf
forms.py
from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.contrib.auth.models import User
from usuario.models import UserProfile
class cadastroForm(UserCreationForm):
...
cpf = forms.CharField(label='CPF')
class Meta:
model = User
fields = ("username", "cpf")
def save(self, commit=True):
...
user.cpf = self.cleaned_data["cpf"]
if commit:
user.save()
return user
views.py
def cadastro(request):
if request.method == 'POST':
form = cadastroForm(request.POST)
if form.is_valid():
new_user = form.save()
return HttpResponseRedirect("/")
else:
form = cadastroForm()
return render_to_response("registration/registration.html", {
'form': form,
})
admin.py (its working fine, dont need any change)
from django.contrib import admin
from usuario.models import UserProfile
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin as AuthUserAdmin
class UserProfileInline(admin.StackedInline):
model = UserProfile
max_num = 1
can_delete = False
class UserAdmin(AuthUserAdmin):
inlines = [UserProfileInline]
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
The problem was in commit conditional, the solution was: