Auto-generating username when adding a user with django - python

I am trying to save username as firstname while adding a user from django admin. Currently it saves None in the username field as I have excluded username in the custom model.
admin.py--
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from .models import UserProfile
from .forms import SignUpForm
class ProfileInline(admin.StackedInline):
model = UserProfile
can_delete = False
verbose_name_plural = 'Profile'
fk_name = 'user'
class CustomUserAdmin(UserAdmin):
inlines = (ProfileInline, )
list_display = ('email', 'first_name', 'last_name', 'is_staff')
list_select_related = ( 'profile', )
exclude = ('username',)
fieldsets = (
('Personal information', {'fields': ('first_name', 'last_name', 'email', 'password')}),
('Permissions', {'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions')}),
('Important dates', {'fields': ('last_login', 'date_joined')}),
)
add_fieldsets = (
('None', {
'classes': ('wide',),
'fields': ('first_name','last_name', 'email', 'password1', 'password2')}
),
)
def get_inline_instances(self, request, obj=None):
if not obj:
return list()
return super(CustomUserAdmin, self).get_inline_instances(request, obj)
admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)
forms.py
from django import forms
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Div, Field
from ajax_select.fields import AutoCompleteSelectField, AutoCompleteField
from phonenumber_field.formfields import PhoneNumberField
from . import models
from captcha.fields import ReCaptchaField
class SignUpForm(forms.Form):
first_name = forms.CharField(max_length=30)
last_name = forms.CharField(max_length=30)
phone_number = PhoneNumberField(label=_("Phone (Please state your country code eg. +44)"))
organisation = forms.CharField(max_length=50)
email = forms.EmailField()
password1 = forms.CharField(max_length=20)
password2 = forms.CharField(max_length=20)
captcha = ReCaptchaField(attrs={'theme' : 'clean'})
def signup(self, request, user):
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
"""
profile, created = models.UserProfile.objects.get_or_create(user=user)
profile.phone_number = self.cleaned_data['phone_number']
profile.organisation = self.cleaned_data['organisation']
profile.save()
user.save()
"""
up = user.profile
up.phone_number = self.cleaned_data['phone_number']
up.organisation = self.cleaned_data['organisation']
user.save()
up.save()
models.py --
from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
from easy_thumbnails.fields import ThumbnailerImageField
from ciasroot.settings import THUMBNAILER_SIZES, UPLOAD_PATH
from ciasroot.constants import GENDERS, LANGUAGES
from ciasroot.util import HashedPk
from phonenumber_field.modelfields import PhoneNumberField
import math, decimal, datetime, os
import uuid
def random_username(sender, instance, **kwargs):
if not instance.username:
instance.username = uuid.uuid4().hex[:30]
models.signals.pre_save.connect(random_username, sender=User)
class UserProfile(models.Model, HashedPk):
user = models.OneToOneField(User, unique=True, related_name ='profile')
job_title = models.CharField(max_length=128, blank=True, null=False, default="")
website = models.URLField(max_length=255, blank=True, null=True)
organisation = models.CharField(max_length=50, blank=True, null=True, default="")
phone_number = PhoneNumberField( blank=True, null=True)
def __str__(self):
return self.user.get_full_name()
def save(self, *args, **kwargs):
super(UserProfile, self).save(*args, **kwargs)
LookupSuggest.add("job_title", self.job_title)
How can I insert username as firstname and make the custom field ie email 'required'. Now the password1 & password2 fields are mandatory.
Any help/link is highly appreciated.

To automatically populate username with the user's first name, you should use a signal - add the following to the models.py where you've defined UserProfile:
def set_username(sender, instance, **kwargs):
if not instance.username:
instance.username = instance.first_name
models.signals.pre_save.connect(set_username, sender=User)
The problem with this is that if you have two users with the same first name, the username won't be unique, so you'll get an integrity error from the database. You could check for uniqueness and append a number until you get a unique value:
def set_username(sender, instance, **kwargs):
if not instance.username:
username = instance.first_name
counter = 1
while User.objects.filter(username=username):
username = instance.first_name + str(counter)
counter += 1
instance.username = username
models.signals.pre_save.connect(set_username, sender=User)
Or, if you're not using the username at all, you could just set it to a random unique value using uuid:
import uuid
def random_username(sender, instance, **kwargs):
if not instance.username:
instance.username = uuid.uuid4().hex[:30]
models.signals.pre_save.connect(random_username, sender=User)
If you plan on using emails for login rather than username, you'll also need to enforce email uniqueness, add the email to the admin user creation form - this should do what you need: https://gist.github.com/gregplaysguitar/1184995

You can define username as a readonly field.
def user_first_name(obj):
return obj.first_name
user_firstname.short_description = 'Firstname'
class CustomUserAdmin(UserAdmin):
readonly_fields = ('user_firstname')
inlines = (ProfileInline, )
list_display = ('email', 'user_first_name', 'last_name', 'is_staff')
list_select_related = ( 'profile', )
exclude = ('username',)
...
For your second question about validating email, define a form for CustomUserAdmin.
class CustomUserAdmin(UserAdmin):
...
form = CustomUserAdminForm
class CustomUserAdminForm(forms.ModelForm):
def clean_email(self):
if not self.cleaned_data['email']:
raise forms.ValidationError("Email is required")
return self.cleaned_data['email']
Or:
class CustomUserAdminForm(forms.ModelForm):
email = forms.EmailField(required=True)

To generate a random unique username use this
import uuid
def random_username(sender, instance, **kwargs):
if not instance.username:
instance.username = uuid.uuid4().hex[:30]
models.signals.pre_save.connect(random_username, sender=settings.AUTH_USER_MODEL)

here is my code
import string
import random
from django.contrib import redirects
from django.contrib.auth.models import User
from django.shortcuts import render
from accounts.models import RegisterIndividualModel
def signup_individual(request):
if request.method == 'POST':
firstname = request.POST['individual-firstname']
lastname = request.POST['individual-lastname']
email = request.POST['individual-email']
password = request.POST['individual-password']
gender = request.POST['individual-gender']
number = request.POST['individual-phone']
# generating random username
letters = string.ascii_uppercase
username = firstname + (''.join(random.choice(letters) for i in range(5)))
new_individual_user = RegisterIndividualModel(firstname=firstname, lastname=lastname, username=username,
email=email, password=password, gender=gender, number=number)
new_individual_user.save()
return render(request, 'feed/feed.html')

Related

how to create a API for user model in Django?

I am Trying To create a API for my custom user model to register a user and get details about current user that is logged in .
I tried to Follow a tutorial but I faced 2 problems, 1.The confirmation password is not being hashed when creating a user, 2. The Get method didn't work ( I was Trying to Get User details Using the id, it didn't return any thing except bad request responce ).
I hope any one can help me.
This is my models.py:
from django.db import models
from django import forms
# Create your models here.
from django.db import models
from django.contrib.auth.models import (
AbstractUser,
BaseUserManager,
AbstractBaseUser
)
from django.forms import Widget
from django.urls import reverse
class CustomUserManager(BaseUserManager):
def create_user(self,email,password=None,is_active=True,is_staff=False,is_admin=False):
if not email:
raise ValueError('User Must Have Email')
if not password:
raise ValueError('User Must Have Password')
user_obj = self.model(
email = self.normalize_email(email)
)
user_obj.set_password(password)#change user password
user_obj.staff = is_staff
user_obj.admin = is_admin
user_obj.active = is_active
user_obj.save(using=self._db)
return user_obj
def create_staffuser(self,email,password=None):
user = self.create_user(
email,
password = password,
is_staff = True
)
return user
def create_superuser(self,email,password=None):
user = self.create_user(
email,
password = password,
is_staff = True,
is_admin = True,
)
return user
class CustomUser(AbstractBaseUser):
username = models.CharField(max_length= 200)
first_name = models.CharField(max_length = 300)
last_name = models.CharField(max_length = 300)
email = models.EmailField(max_length=255,unique=True)
password = models.CharField(max_length=100,)
password_2 = models.CharField(max_length=100,)
sub_to_newsletter = models.BooleanField(default=True)
own_pc = models.BooleanField(default=False)
active = models.BooleanField(default=True) #can login
staff = models.BooleanField(default=False) #staff user not superuser
admin = models.BooleanField(default=False) #admin / superuser
USERNAME_FIELD = 'email' #username
#email and password is requierd by default
REQUIRED_FIELDS = [] #python manage.py createsuperuser
objects = CustomUserManager()
def __str__(self):
return self.email
def get_full_name(self):
return self.email
def get_short_name(self):
return self.email
def has_perm(self,perm,obj=None):
return True
def has_module_perms(self,app_label):
return True
#property
def is_staff(self):
return self.staff
#property
def is_admin(self):
return self.admin
#property
def is_active(self):
return self.active
def get_absolute_url(request):
return reverse('')
This is my forms.py:
from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import ReadOnlyPasswordHashField
User = get_user_model()
class UserAdminCreationForm(forms.ModelForm):
"""
A form for creating new users. Includes all the required
fields, plus a repeated password.
"""
password = forms.CharField(widget=forms.PasswordInput())
password_2 = forms.CharField(label='Confirm Password', widget=forms.PasswordInput())
class Meta:
model = User
fields = ['email']
def clean(self):
'''
Verify both passwords match.
'''
cleaned_data = super().clean()
password = cleaned_data.get("password")
password_2 = cleaned_data.get("password_2")
if password is not None and password != password_2:
self.add_error("password_2", "Your passwords must match")
return cleaned_data
def save(self, commit=True):
# Save the provided password in hashed format
user = super().save(commit=False)
user.set_password(self.cleaned_data["password"])
if commit:
user.save()
return user
class UserAdminChangeForm(forms.ModelForm):
"""A form for updating users. Includes all the fields on
the user, but replaces the password field with admin's
password hash display field.
"""
password = ReadOnlyPasswordHashField()
class Meta:
model = User
fields = ['email', 'password']
def clean_password(self):
# Regardless of what the user provides, return the initial value.
# This is done here, rather than on the field, because the
# field does not have access to the initial value
return self.initial["password"]
This is admin.py:
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from .models import *
from .forms import *
User = get_user_model()
# Register your models here.
class UserAdmin(BaseUserAdmin):
# The forms to add and change user instances
form = UserAdminChangeForm
add_form = UserAdminCreationForm
# The fields to be used in displaying the User model.
# These override the definitions on the base UserAdmin
# that reference specific fields on auth.User.
list_display = ['email', 'admin']
list_filter = ['admin']
fieldsets = (
(None, {'fields': ('email', 'password')}),
('Personal info', {'fields': ('username','first_name','last_name','sub_to_newsletter','own_pc')}),
('Permissions', {'fields': ('admin',)}),
)
# add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
# overrides get_fieldsets to use this attribute when creating a user.
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('email', 'password', 'password_2')}
),
)
search_fields = ['email']
ordering = ['email']
filter_horizontal = ()
admin.site.register(User,UserAdmin)
I suggest that you remove password_2 from your user model. There is no reason to store the password twice. The purpose of a "confirm password" is to force the user to enter the same password twice in order to eliminate typos.
You can keep password_2 as a form field. Then add field validation to check that password and password_2 are the same.

I am trying to create a custom user in my Django application but I am facing some Unknown problem when I tried my custom user

##My django codes are:
from django.contrib import admin
from django import forms
from django.contrib.auth.models import Group
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.forms import ReadOnlyPasswordHashField
from django.core.exceptions import ValidationError
from django.contrib.auth import get_user_model
# Register your models here.
from .models import MyUser
class UserCreationForm(forms.ModelForm):
password1 = forms.CharField(label="password", widget=forms.PasswordInput)
password2 = forms.CharField(
label="password confirmation", widget=forms.PasswordInput)
class Meta:
model = MyUser
fields = ('username', "email", "first_name", "last_name",
"gender", "date_of_birth")
def clean_password2(self):
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise ValidationError("Password don't match")
return password2
def save(self, commit=True):
user = super().save(commit=False)
user.set_password(self.cleaned_data["password1"])
if commit:
user.save()
return user
class UserChangeForm(forms.ModelForm):
passwrod = ReadOnlyPasswordHashField()
class Meta:
model = MyUser
fields = ('username', "email", "first_name", "last_name",
"gender", "date_of_birth", 'is_active', "is_admin")
def clean_password(self):
return self.initial["password"]
class UserAdmin(BaseUserAdmin):
form = UserChangeForm
add_form = UserCreationForm
list_display = ('username', 'email', 'first_name',
'last_name', 'is_admin', 'is_active')
list_filter = ('is_admin',)
fieldsets = (
(None, {
"fields": ('username', 'email', 'first_name',
'last_name', 'is_admin', 'is_active', 'password', 'date_of_birth', 'gender'),
}),
)
add_fieldsets = (
(None, {
"fields": ('username', 'email', 'first_name',
'last_name', 'is_admin', 'is_active', 'password1', 'password2', 'date_of_birth', 'gender'),
}),
)
search_fields = ('email',)
ordering = ('email',)
filter_horizontal = ()
User = get_user_model()
admin.site.register(User, UserAdmin)
##my models.py
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
# Create your models here.
class MyUserManager(BaseUserManager):
def create_user(self, username, email, first_name, last_name, date_of_birth, gender, password=None):
if not first_name:
raise ValueError("Users must have a first name")
if not last_name:
raise ValueError("Users must have a last name")
if not email:
raise ValueError('Users must have an email address')
if not gender:
raise ValueError("Users must have a gender")
if not date_of_birth:
raise ValueError("You Must Enter your date of birth")
user = self.model(
username=username,
email=self.normalize_email(email),
first_name=first_name,
last_name=last_name,
date_of_birth=date_of_birth,
gender=gender,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, username, email, first_name, last_name, date_of_birth, gender, password=None):
user = self.create_user(
username,
email,
first_name,
last_name,
date_of_birth,
gender,
password=password,
)
user.is_admin = True
user.save(using=self._db)
return user
class MyUser(AbstractBaseUser):
username = models.CharField(max_length=50, unique=True)
email = models.EmailField(max_length=254, unique=True)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
date_of_birth = models.DateField()
gender_choices = [
("male", "Male"),
("female", "Female"),
("others", "Others")
]
gender = models.CharField(max_length=50, choices=gender_choices)
is_superuser = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = MyUserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['first_name',
'email', 'last_name', 'date_of_birth', 'gender']
def __str__(self):
return self.username
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return True
#property
def get_full_name(self):
return f"{self.first_name} {self.last_name}"
#property
def is_staff(self):
return self.is_admin
#MY urls.py
from django.urls import path
from . import views
urlpatterns = [
path('users', views.userList, name="users"),
path('login', views.logIn, name="login"),
path('login-admin', views.adminLogin, name="login-admin"),
path("register", views.registerUser, name="register")
]
#my views
from django.shortcuts import render, redirect
from .form import AddForm
from .models import AddProduct, Order, OrderItem, Clint
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.models import User
# from django.contrib.auth.decorators import login_required
# Create your views here.
# #login_required
def index(request):
allProduct = AddProduct.objects.count()
lowOnStock = AddProduct.objects.filter(
product_quantity__lte=5, product_quantity__gt=0).count()
user = get_user_model()
print(user.email)
outOfStock = AddProduct.objects.filter(product_quantity=0).count()
return render(request, "index.html", {"allProduct": allProduct, "los": lowOnStock, "oos": outOfStock, "user": user})
# #login_required
def productList(request):
march = AddProduct.objects.all()
return render(request, "productList.html", {"march": march})
# #login_required
def addProduct(request):
if request.method == "GET":
return render(request, "addProduct.html", {"form": AddForm()})
else:
form = AddForm(request.POST, request.FILES)
if form.is_valid:
form.save()
return redirect("/productList")
# #login_required
def deleteProduct(request, pk):
delProduct = AddProduct.objects.get(id=pk)
if request.method == "POST":
delProduct.delete()
return redirect("/productList")
else:
return render(request, "delConform.html", {"item": delProduct})
# #login_required
def updateProduct(request, pk):
upProduct = AddProduct.objects.get(id=pk)
form = AddForm(instance=upProduct)
if request.method == "POST":
form = AddForm(request.POST, request.FILES, instance=upProduct)
upProduct.img.delete()
form .save()
return redirect("/")
return render(request, "updateProduct.html", {"form": form})
# #login_required
def addInfo(request):
if request.method == "GET":
context = {}
searchInput = request.GET.get("searchInput")
if searchInput == None:
context['products'] = AddProduct.objects.all()
else:
context['products'] = AddProduct.objects.filter(
product_name__icontains=searchInput)
context["orders"] = Order.objects.all()
context["orderItems"] = OrderItem.objects.all()
context["customers"] = Clint.objects.all()
return render(request, "management.html", context)
Note: when I tried to print the user for checking purpose I got the unknown object <django.db.models.query_utils.DeferredAttribute object at 0x0000019720E0CDC0>
def index(request):
allProduct = AddProduct.objects.count()
lowOnStock = AddProduct.objects.filter(
product_quantity__lte=5, product_quantity__gt=0).count()
user = get_user_model()
print(user.email)
outOfStock = AddProduct.objects.filter(product_quantity=0).count()
return render(request, "index.html", {"allProduct": allProduct, "los": lowOnStock, "oos": outOfStock, "user": user})
user = get_user_model() print(user.email)
I don't user.email insted I got this unknown object <django.db.models.query_utils.DeferredAttribute object at 0x0000019720E0CDC0>
#I tried to solved the object #<django.db.models.query_utils.DeferredAttribute object at 0x0000019720E0CDC0> By putting it into user variable and
calling its fields but it didn't work like:
def index(request):
allProduct = AddProduct.objects.count()
lowOnStock = AddProduct.objects.filter(
product_quantity__lte=5, product_quantity__gt=0).count()
user = get_user_model()
print(user.email)
outOfStock = AddProduct.objects.filter(product_quantity=0).count()
return render(request, "index.html", {"allProduct": allProduct, "los": lowOnStock, "oos": outOfStock, "user": user})
user = get_user_model() print(user.email) I don't user.email insted I got this unknown object <django.db.models.query_utils.DeferredAttribute object at 0x0000019720E0CDC0>
user = get_user_model() print(user.email)
I didn't get the printed result insted i got <django.db.models.query_utils.DeferredAttribute object at 0x0000019720E0CDC0> this object
but the I tried to add .first() ofter get_user_model
it worked I got my result
user = get_user_model().first()
print(user.email)
So try to use the .first() after model if you are facing this kind of problem

User Level Registration in Django

I am trying to create two user levels in Django. When they register, they will see a dropdown field where they can choose whether they are a customer or a driver.
models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
is_customer = models.BooleanField(default=False)
is_driver = models.BooleanField(default=False)
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
address = models.CharField(max_length=300)
contact_no = models.CharField(max_length=11)
profile_pic = models.ImageField(blank=True, null=True)
def __str__(self):
return self.user.username
views.py
def RegisterPage(request):
form = RegisterForm()
if request.method == "POST":
form = RegisterForm(request.POST)
if form.is_valid():
form.save()
context = {'form':form}
return render(request, 'accounts/register.html', context)
forms.py
from django.contrib.auth.forms import UserCreationForm
from .models import *
from django import forms
from django.db import transaction
class RegisterForm(UserCreationForm):
email = forms.EmailField()
first_name = forms.CharField(max_length=60)
last_name = forms.CharField(max_length=150)
address = forms.CharField(max_length=300)
contact_no = forms.CharField(max_length=11)
profile_pic = forms.ImageField(required=False)
LEVELS = (
('Customer', 'Customer'),
('Driver', 'Driver'),
)
user_level = forms.ChoiceField(choices=LEVELS)
class Meta(UserCreationForm.Meta):
model = User
fields = ['username', 'email', 'first_name', 'last_name',
'password1', 'password2', 'address', 'contact_no',
'profile_pic', 'user_level']
#transaction.atomic
def save(self, commit=True):
user = super().save(commit=False)
level = self.cleaned_data.get('user_level')
if level == 'Customer':
user.is_customer = True
if commit:
user.save()
elif level == 'Driver':
user.is_driver = True
if commit:
user.save()
profile = Profile.objects.create(user=user)
profile.address = self.cleaned_data.get('address')
profile.contact_no = self.cleaned_data.get('contact_no')
profile.profile_pic = self.cleaned_data.get('profile_pic')
profile.save()
return user
I have registered my user model to the Django settings already. My problem is that it doesn't save to the User model when I check it in the admin page. What seems to be the problem?

Django models RelatedObjectDoesNotExist

i am making an ecom app for Django but when i use a diffrent user i get this error. i think it has something to do with assigning a Customer to a User, as when i manually create a Customer with a User.
Exception Type: RelatedObjectDoesNotExist
Exception Value:
User has no customer.
Exception Location: L:\Django\Website\lib\site-packages\django\db\models\fields\related_descriptors.py in __get__, line 420
Python Executable: L:\Django\Website\Scripts\python.exe
Python Version: 3.8.3
here is my views.py
from django.shortcuts import render
from .models import *
from django.http import JsonResponse
import json
import datetime
from django.contrib.auth.models import User
def store(request):
if request.user.is_authenticated:
customer = request.user.customer
order, created = Order.objects.get_or_create(customer=customer, complete=False)
items = order.orderitem_set.all()
cartItems = order.get_cart_items
else:
items = []
order = {'get_cart_total':0,'get_cart_items':0, 'shipping':False}
cartItems = order['get_cart_items']
search = request.GET.get('search')
products = Product.objects.all()
if search != '' and search is not None:
products = products.filter(name__icontains=search)
context= {'products': products, 'cartItems':cartItems}
return render(request, 'store/store.html', context)
...unrelated...
here is my models.py
from django.db import models
from django.contrib.auth.models import User
class Customer(models.Model):
user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
name = models.CharField(max_length=200, null=True)
email = models.CharField(max_length=200)
def __str__(self):
return self.name
...unrelated...
========================
when i use the method in the code block it doesn't save the extra fields
forms.py
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from .models import Profile
class UserRegisterForm(UserCreationForm):
email = forms.EmailField(required=True)
first_name = forms.CharField(required=True)
last_name = forms.CharField(required=True)
class meta:
model = User
fields = ['username', 'first_name', 'last_name', 'email', 'password1', 'password2']
views.py
def register(request):
if request.method == 'POST':
form = UserRegisterForm(request.POST, request.FILES)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
messages.success(request, f'Your account has been created! You are now able to login')
return redirect('login')
else:
form = UserRegisterForm()
return render(request, 'users/register.html', {'form': form})
but when i try this it saves everything except the passwords
forms.py
class UserRegisterForm(UserCreationForm):
email = forms.EmailField(required=True)
first_name = forms.CharField(required=True)
last_name = forms.CharField(required=True)
class meta:
model = User
fields = ['username', 'first_name', 'last_name', 'email', 'password1', 'password2']
def save(self, **kwargs):
user = super(UserCreationForm, self).save(commit=False)
user.email = self.cleaned_data["email"]
user.first_name = self.cleaned_data["first_name"]
user.last_name = self.cleaned_data["last_name"]
user.save()
return user
views.py is the same.
I'd suggest to use the Customer model as what you refer to as the user throughout the app. Therefore I would keep the save method for the user as default and add the extra fields to the Customer model.
This makes it also easier to later add extra data of the user when registering. The Customer would be created on registering the user. For example:
class UserRegisterForm(UserCreationForm):
email = forms.EmailField(required=True)
first_name = forms.CharField(required=True)
last_name = forms.CharField(required=True)
def save(self, **kwargs):
user = super().save()
customer = Customer.objects.create(user=user, first_name=self.cleaned_data.get('first_name'),
last_name=self.cleaned_data.get('last_name'), email=self.cleaned_data.get('email'))
return user
The Customer model will need a first_name and last_name field instead of name now.
Your current code (store view) will then work as well.

table services_parceiros has no column named user_id, DJANGO

thanks for your time.
i've been trying to set a model to his creator user. although i'm getting this error and would like to know if am'i doing anything wrong before try a AbstractUser. i'did my user app just in the forms.py i don't know if thats the reason.
Error:
table services_parceiros has no column named user_id
accounts/forms.py:
from django import forms
from django.contrib.auth import get_user_model, authenticate
User = get_user_model()
class LoginForm(forms.Form):
username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput())
def clean (self, *args, **kwargs):
username = self.cleaned_data.get('username')
password = self.cleaned_data.get('password')
if username and password:
user = authenticate(username=username, password=password)
if not user:
raise forms.ValidationError('usuario não encontrado')
if not user.check_password(password):
raise forms.ValidationError('senha incorreta')
if not user.is_active:
raise forms.ValidationError('usuario desativado')
return super(LoginForm, self).clean(*args, **kwargs)
class RegisterForm(forms.ModelForm):
first_name = forms.CharField(max_length=100)
last_name = forms.CharField(max_length=100)
email = forms.EmailField(label='Email address')
email2 = forms.EmailField(label='Confirm Email')
password = forms.CharField(widget=forms.PasswordInput)
class Meta:
model = User
fields = [
'first_name',
'last_name',
'username',
'email',
'email2',
'password'
]
def clean(self, *args, **kwargs):
email = self.cleaned_data.get('email')
email2 = self.cleaned_data.get('email2')
if email != email2:
raise forms.ValidationError("Emails must match")
email_qs = User.objects.filter(email=email)
if email_qs.exists():
raise forms.ValidationError(
"This email has already been registered")
return super(RegisterForm, self).clean(*args, **kwargs)
services/models.py:
from django.db import models
from phone_field import PhoneField
from datetime import datetime
from django.contrib.auth import get_user_model
from django.template.defaultfilters import slugify
from accounts.forms import User
get_user_model = User
class Parceiros (models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
nome = models.CharField(max_length=200)
endereco = models.TextField(max_length=400, blank=True)
responsavel = models.CharField(max_length=100)
tel = PhoneField(max_length=12)
created_at = models.DateTimeField(auto_now=True)
updated_at = models.DateTimeField(auto_now_add=True, blank=True)
ativo = models.BooleanField(default=False)
def __str__(self):
return '%s %s' % (self.user, self.nome)
and services/views.py:
from django.shortcuts import render, redirect
from .models import Servicos, Parceiros, Imagens
from django.views.generic import UpdateView, DetailView, ListView
from .forms import ParceirosForm, ServicosForm, ImagensForm
from django.contrib.auth.decorators import login_required
from django.contrib.auth.context_processors import auth
def home_view(request):
serv = Servicos.objects.all()
context = {'serv': serv }
return render (request, 'home.html', context)
#login_required
def parceiros_create(request):
if request.method == 'POST':
form = ParceirosForm(request.POST)
if form.is_valid():
parceiro = form.save(commit=False)
parceiro.user = request.user # assuming user is a FK field on Parceiros#
parceiro.save()
return redirect('home2')
else:
form = ParceirosForm()
context = {
'form': form,
}
return render (request, 'parceiroform.html', context)
def get_queryset(self):
return super().get_queryset().filter(parceiro__user=self.request.user)
services/forms.py:
if i specify the user on fields of my ParceirosForm i'll get the field displayed and still gets the same error when submit.
from django import forms
from .models import Servicos, Imagens, Parceiros
from phone_field import PhoneField
class ParceirosForm(forms.ModelForm):
nome = forms.CharField(max_length=200)
endereco = forms.TextInput()
responsavel = forms.CharField(max_length=100)
tel = PhoneField(max_length=12)
class Meta:
prefix = 'parceiro'
model = Parceiros
fields = ['nome', 'endereco', 'responsavel', 'tel']
0001_initial.py:
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import phone_field.models
import services.models
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='Parceiros',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('nome', models.CharField(max_length=200)),
('endereco', models.TextField(blank=True, max_length=400)),
('responsavel', models.CharField(max_length=100)),
('tel', phone_field.models.PhoneField(max_length=12)),
('created_at', models.DateTimeField(auto_now=True)),
('updated_at', models.DateTimeField(auto_now_add=True)),
('ativo', models.BooleanField(default=False)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),

Categories

Resources