I created an app using ./manage.py startapp profiles then in the app i create a file called forms.py and it looks like this
from django import forms
from django.contrib.auth import get_user_model
from .models import Profile
User = get_user_model()
class ProfileForm(forms.ModelForm):
first_name = forms.CharField(required=False)
last_name = forms.CharField(required=False)
email = forms.CharField(required=False)
class Meta:
model = Profile
fields = ['location', 'bio']
in views.py
when I try from .forms import ProfileForm it shows error says
from .forms import ProfileForm
ModuleNotFoundError: No module named 'profiles.forms'
here is my code at users/admin.py,
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from store.models import Customer
from .forms import CustomUserCreationForm, CustomUserChangeForm
CustomUser = get_user_model()
class CustomUserAdmin(UserAdmin):
add_form = CustomUserCreationForm
form = CustomUserChangeForm
model = CustomUser
list_display = ['email', 'username',]
admin.site.register(CustomUser, CustomUserAdmin)
Also users/forms.py code is,
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
class CustomUserCreationForm(UserCreationForm):
class Meta:
model = get_user_model()
fields = ('email', 'username',)
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = get_user_model()
fields = ('email', 'username',)
I have table on store/models.py ,
class Customer(models.Model):
user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, null=True, blank=True,unique=True)
#name = models.CharField(max_length=200, null=True)
email = models.EmailField(max_length=200, null=True)
def __str__(self):
return self.user.username
Now can anyone please suggest me how to create a customer instances in user/admins.py or how to save data to customer table.
I am taking online Djago class and have no one to ask for help. I would appreciate to get any help and tips.
I am learning creating forms (login/singup).
So far i did in models.py
from django.db import models
from django.contrib import auth
class User(auth.models.User,auth.models.PermissionsMixin):
def __str__(self):
return "#{}".format(self.username)
In forms.py
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm
class UserCreateForm(UserCreationForm):
class Meta:
fields = ('username','email','password1','password2')
model = get_user_model()
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
self.fields['username'].label = 'Display Name'
self.fields['email'].label = 'Email Address'
I want to add addtional firelds as surname , however when i add 'surname' in class meta in fields (forms.py) i get error, however the online training says i can easily add additional fields. Could you please tell what i am doing wrong and how i can add this field?
when i add those field 'surname' in forms.py:
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm
class UserCreateForm(UserCreationForm):
class Meta:
fields = ('username','email','surname','password1','password2')
model = get_user_model()
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
self.fields['username'].label = 'Display Name'
self.fields['surname'].label = 'Surname'
self.fields['email'].label = 'Email Address'
it give the following error:
raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (surname) specified for User
In views.py i ahve:
from django.shortcuts import render
from django.urls import reverse_lazy
from django.views.generic import CreateView,TemplateView
from . import forms
class SignUp(CreateView):
form_class = forms.UserCreateForm
success_url = reverse_lazy('login')
template_name = 'accounts/signup.html'
# In your forms.py, you need to add new field 'surname'
For Example:-
from django import forms
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm
class UserCreateForm(UserCreationForm):
surname = forms.CharField(max_length=20)
class Meta:
fields = ('username','email','surname','password1','password2')
model = get_user_model()
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
self.fields['username'].label = 'Display Name'
self.fields['surname'].label = 'Surname'
self.fields['email'].label = 'Email Address'
First of all you are using the get_user_model() method but you are working with a custom User model.
In you settings set AUTH_USER_MODEL:
AUTH_USER_MODEL = 'app.User'
Then add the surname field to your User model:
class User(auth.models.User):
surname = models.Charfield()
def __str__(self):
return "#{}".format(self.username)
Run and apply migration. Then you will be able to add the surname field to your form fields
class UserCreateForm(UserCreationForm):
class Meta:
fields = ('username','email','surname','password1','password2')
model = get_user_model()
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
self.fields['username'].label = 'Display Name'
self.fields['surname'].label = 'Surname'
self.fields['email'].label = 'Email Address'
You need to make following changes to your forms.py file:-
from appname.models import USER
from django.contrib.auth.forms import UserCreationForm
class UserCreateForm(UserCreationForm):
class Meta:
fields = ('username','email','surname','password1','password2')
model = USER
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
self.fields['username'].label = 'Display Name'
self.fields['surname'].label = 'Surname'
self.fields['email'].label = 'Email Address'
I want to create a signup form for a Django custom user model using abstractuser. I don't want anything special, just the ability to later on add custom fields if needed.
I understand from the docs that I need to:
set AUTH_USER_MODEL
define a custom user model by subclassing AbstractUser
define a custom model manager by subclassing UserManager
subclass UserCreationForm and UserChangeForm
Here's my current code:
My settings.py:
AUTH_USER_MODEL = 'users.CustomUser'
My models.py:
from django.contrib.auth.models import AbstractUser, UserManager
class CustomUserManager(UserManager):
pass
class CustomUser(AbstractUser):
objects = CustomUserManager()
My forms.py:
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser
class CustomUserCreationForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = CustomUser
fields = UserCreationForm.Meta.fields
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = CustomUser
fields = UserChangeForm.Meta.fields
My admin.py:
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from .forms import CustomUserCreationForm, CustomUserChangeForm
from .models import CustomUser
class CustomUserAdmin(UserAdmin):
model = CustomUser
add_form = CustomUserCreationForm
form = CustomUserChangeForm
admin.site.register(CustomUser, CustomUserAdmin)
My views.py:
from django.urls import reverse_lazy
from django.views import generic
from .forms import CustomUserCreationForm
class SignUp(generic.CreateView):
form_class = CustomUserCreationForm
success_url = reverse_lazy('login')
template_name = 'signup.html'
Upon submission of the signup form get the error message:
OperationalError at /users/signup/
no such table: users_customuser
I've nuked my database and done makemigrations and then migrate from scratch. Something is wrong with my form and perhaps admin, too.
See the Django docs about rewriting the User model: Referencing the User Model
However, if your user model extends AbstractBaseUser, you’ll need to define a custom ModelAdmin class.
In admin.py, you should get the user model from django.contrib.auth:
admin.py:
from django.contrib.auth import get_user_model
Class MyCustomModelAdmin...
CustomUser = get_user_model()
admin.site.register(CustomUser, MyCustomModelAdmin)
EDIT: I think the issue may be with your use of UserCreationForm.
My forms.py:
from django import forms
from .models import CustomUser
class CustomUserCreationForm(UserCreationForm):
class Meta:
model = CustomUser
fields = [list fields here]
my admin page does not show the fields listed in admin.py.
Here is the admin.py
from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from customRegistration.models import ExUserProfile
admin.site.unregister(User)
class UserProfileInline(admin.StackedInline):
model = ExUserProfile
class UserProfileAdmin(UserAdmin):
inlines = [ UserProfileInline, ]
list_display = ('username', 'email','dateofBirth')
admin.site.register(User, UserProfileAdmin)
I do not see the username and dateofBirth field on my admin page.
models.py:
from django.db import models
from registration.models import User
from registration.signals import user_registered
class ExUserProfile(models.Model):
user = models.ForeignKey(User, unique=True)
ishuman = models.BooleanField(required=True)
dateofBirth=models.DateField(required=True)
def __unicode__(self):
return self.user
def user_registered_callback(sender, user, request, **kwargs):
profile = ExUserProfile(user = user)
profile.ishuman = bool(request.POST["ishuman"])
profile.dateofBirth=request.POST["dateofBirth"]
print request.POST["dateofBirth"]
profile.save()
user_registered.connect(user_registered_callback)