I created a user profile model for my system. I created all models and it works perfectly. I have a form, and the form works too. But when I look user create form from admin page, it doesn't look the same.
There are some missing parts like rank, comp_name. How can I fix it?
models.py
class UserProfile(models.Model):
ranks = (
('xxx', 'xxx'),
...
)
comp_name = models.CharField(max_length=200, default="Choose")
user_id = models.UUIDField(default=uuid.uuid4(), editable=False, unique=True)
username = models.CharField(max_length=500)
first_name = models.CharField(max_length=200, default=None)
last_name = models.CharField(max_length=200, default=None)
password = models.CharField(max_length=50)
email = models.EmailField(max_length=254)
rank = models.CharField(max_length=200, choices=ranks)
forms.py
class SignUpForm(UserCreationForm):
comp_name = forms.CharField(label='What is your company name?')
email = forms.CharField(max_length=254)
rank = forms.ChoiceField(label='What is your rank?', choices=UserProfile.ranks)
first_name = forms.CharField(max_length=250)
last_name = forms.CharField(max_length=250)
comp_name = forms.ModelChoiceField(queryset=CompanyProfile.objects.all())
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email', 'comp_name', 'password1', 'password2', 'rank'
admin
admin panel "Change User" screen
In forms.py
class SignUpForm(UserCreationForm):
comp_name = forms.CharField(label='What is your company name?')
email = forms.CharField(max_length=254)
rank = forms.ChoiceField(label='What is your rank?', choices=UserProfile.ranks)
first_name = forms.CharField(max_length=250)
last_name = forms.CharField(max_length=250)
comp_name = forms.ModelChoiceField(queryset=CompanyProfile.objects.all())
class Meta:
model = User --> change to UserProfile
fields = ('username', 'first_name', 'last_name', 'email', 'comp_name', 'password1', 'password2', 'rank'
Related
I currently have a single page signup form implemented with allauth
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
email = models.EmailField(_('Professional email address'), unique=True)
username = models.CharField(_("User Name"), blank=False, max_length=255, unique=True)
first_name = models.CharField(_("First Name"), null=True, max_length=255, default='')
last_name = models.CharField(_("Last Name"), null=True, max_length=255, default='')
country = CountryField(_("Country of Practice"), blank_label='(Country of Practice)', blank = False, default='GB')
terms = models.BooleanField(verbose_name=_('I have read and agree to the terms and conditions'), default=False)
def get_absolute_url(self):
return reverse(
"users:detail", kwargs={"username": self.username}
)
objects = UserManager()
And this is the forms.py
class UserCreationForm(forms.UserCreationForm):
error_message = forms.UserCreationForm.error_messages.update(
{"duplicate_username": _("This username has already been taken.")}
)
username = CharField(label='User Name',
widget=TextInput(attrs={'placeholder': 'User Name'}))
class Meta(forms.UserCreationForm.Meta):
model = User
fields = ['username', 'email', 'first_name', 'last_name', 'password1', 'password2', 'terms']
field_order = ['username', 'email', 'first_name', 'last_name', 'password1', 'password2', 'terms']
def clean_terms(self):
is_filled = self.cleaned_data['terms']
if not is_filled:
raise forms.ValidationError('This field is required')
return is_filled
def clean_username(self):
username = self.cleaned_data["username"]
if self.instance.username == username:
return username
try:
User._default_manager.get(username=username)
except User.DoesNotExist:
return username
raise ValidationError(
self.error_messages["duplicate_username"]
)
I would like however for the first sign up page to have a ‘next’ button at the bottom and then there would be a second page where the user input separate details (the data input here might vary based on the inputs in the first page). The Django ‘form tools’ form wizard seems well suite to this but I can’t work out how to integrate it with all auth
Any suggestions much appreciated
I'm fairly new with the django restframework.
I am trying to create a serializer and a view for a model that has a foreignKey.
Models.py
class Job(models.Model):
"""A Job used to create a job posting"""
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
)
description = models.TextField()
job_type = models.CharField(max_length=12, choices=JOB_TYPE_CHOICES, default='Full-Time')
city = models.CharField(max_length=255)
state = models.CharField(max_length=255)
created_date = models.DateField(auto_now=False, auto_now_add=True)
salary = models.CharField(max_length=255)
position = models.CharField(max_length=255)
employer = models.CharField(max_length=255)
is_active = models.BooleanField(default=True)
def __str__(self):
return self.description[:50]
class Applicant(models.Model):
"""A applicant that can apply to a job"""
job = models.ForeignKey(Job, on_delete=models.CASCADE)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
email = models.EmailField(max_length=254)
phone_number = PhoneNumberField()
resume = models.FileField(upload_to=resume_file_path, validators=[validate_file_extension])
def __str__(self):
return self.first_name
The idea is that Applicant will be able to apply to Job.
I can't seem to figure out how to get the Job id when a Applicant is applying to job.
serializers.py
class JobSerializer(serializers.ModelSerializer):
"""Serializer for tag objects"""
class Meta:
model = Job
fields = ('id', 'description', 'job_type', 'city', 'state', 'salary', 'position', 'employer', 'created_date', 'is_active')
read_only_fields = ('id',)
def create(self, validated_data):
"""Create a job posting with user and return it"""
return Job.objects.create(**validated_data)
class ApplyJobSerializer(serializers.ModelSerializer):
"""Serializer for applying to jobs"""
class Meta:
model = Applicant
fields = ('id', 'first_name', 'last_name', 'email', 'phone_number', 'resume')
read_only_fields = ('id',)
views.py
class ApplyJobView(generics.CreateAPIView):
"""Allows applicants to apply for jobs"""
serializer_class = serializers.ApplyJobSerializer
Below is an image of my http://localhost:8000/api/jobPosting/apply/ url
I don't know how i can bring in the Jobs as in option to my view so I can obtain the id field.
I get the following error when POST
null value in column "job_id" violates not-null constraint
You are not passing job field in serializer fields. Try this:
class ApplyJobSerializer(serializers.ModelSerializer):
"""Serializer for applying to jobs"""
class Meta:
model = Applicant
fields = ('id', 'first_name', 'last_name', 'email', 'phone_number', 'resume', 'job')
read_only_fields = ('id',)
I'm working on a project using Python(3.7) and Django(2.2) in which I have implemented my models for multiple user types with custom user model as the base model. Everything working fine except the admin side, I have register these modles to admin but when I try to add an object from admin interface it's giving an error.
Here's what I have tried so far:
From models.py:
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(max_length=254, unique=True)
title = models.CharField(max_length=255, blank=False)
user_type = models.CharField(max_length=255, choices=USER_TYPE, blank=False)
gender = models.CharField(max_length=255, choices=CHOICES, blank=False)
contenst = models.CharField(max_length=255, blank=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
last_login = models.DateTimeField(null=True, blank=True)
date_joined = models.DateTimeField(auto_now_add=True)
USERNAME_FIELD = 'email'
EMAIL_FIELD = 'email'
REQUIRED_FIELDS = ['password']
objects = UserManager()
def get_absolute_url(self):
return "/users/%i/" % (self.pk)
class PersonalBelow18(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
dob = models.DateField(blank=False)
customer_id = models.BigIntegerField(blank=False)
collection_use_personal_data = models.BooleanField(blank=False)
reference_identities = models.ForeignKey(Identities, blank=False, on_delete=models.CASCADE, related_name='refs')
def __str__(self):
return self.user.email+'\'s account with ' + str(self.customer_id)
class PersonalAbove18(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
dob = models.DateField(blank=False)
customer_id = models.BigIntegerField(blank=False)
contact_email = models.EmailField(blank=False)
reference_identities = models.ForeignKey(Identities, blank=False, on_delete=models.CASCADE)
contact_no = PhoneNumberField(blank=True, help_text='Phone number must be entered in the'
'format: \'+999999999\'. Up to 15 digits allowed.')
collection_use_personal_data = models.BooleanField(blank=False)
def __str__(self):
return self.user.email+'\'s account with ' + str(self.customer_id)
class Parent(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
contact_email = models.EmailField(blank=False)
customer_id = models.BigIntegerField(blank=True)
contact_no = PhoneNumberField(blank=True, help_text='Phone number must be entered in the'
'format: \'+999999999\'. Up to 15 digits allowed.')
collection_use_personal_data = models.BooleanField(blank=False)
class GroupContactPerson(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
contact_email = models.EmailField(blank=False)
customer_id = models.BigIntegerField(blank=False)
contact_no = PhoneNumberField(blank=True, help_text='Phone number must be entered in the'
'format: \'+999999999\'. Up to 15 digits allowed.')
department = models.CharField(max_length=255, blank=False)
address = models.TextField(max_length=255, blank=False)
and here's how I register these models to admin:
From admin.py:
class UserAdmin(BaseUserAdmin):
fieldsets = (
(None, {'fields': ('email', 'password', 'title', 'user_type',
'gender', 'contenst', 'last_login')}),
('Permissions', {'fields': (
'is_active',
'is_staff',
'is_superuser',
'groups',
'user_permissions',
)}),
)
add_fieldsets = (
(
None,
{
'classes': ('wide',),
'fields': ('email', 'password1', 'password2')
}
),
)
list_display = ('email', 'title', 'is_staff', 'last_login')
list_filter = ('is_staff', 'is_superuser', 'is_active', 'groups')
search_fields = ('email',)
ordering = ('email',)
filter_horizontal = ('groups', 'user_permissions',)
admin.site.register(User, UserAdmin)
admin.site.register(PersonalBelow18)
admin.site.register(PersonalAbove18)
admin.site.register(Parent)
admin.site.register(GroupContactPerson)
The Parent and GroupContactPerson models are working well on admin side but the when I try to add an object for PersonalBelow18 & PersonalAbove18 models, it gives the following error as:
TypeError: str returned non-string (type int)
Here's how I debug this problem in these models:
I start removing all fields one-by-one
Remove a field from model & form and perform migrations
Then test the admin
Then I found that when I removed the reference_identities field it start working, so I get that this model was returning an integer, so I fixed that model and it fix the issue.
In short, it's a good approach to find a path to the actual place of problem by removing fields one-by-one and test the admin.
I have created a registration form using Django's usercreation form.
Then i created a EditProfileForm using the registration form.
My issue is that EditProfileForm uses User Model, and the Birthdate field is in Profile Model. Thus, I am able to edit all fields except Birthdate as it is not from the User Model.
How do I go about editing birthdate, or creating a form where I can edit all fields instead of only the user fields?
My Model for Profile:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.PROTECT)
def __str__(self):
return self.user.username;
USWEST = 'usw'
USEAST = 'use'
EUROPE = 'eu'
OCEANIA = 'oce'
ASIA = 'as'
SOUTHAMERICA = 'sam'
SOUTHAFRICA = 'saf'
MIDDLEEAST = 'me'
PREF_SERVER_CHOICES = (
(USWEST, 'US-West'),
(USEAST, 'US-East'),
(EUROPE, 'Europe'),
(OCEANIA, 'Oceania'),
(ASIA, 'Asia'),
(SOUTHAMERICA, 'South America'),
(SOUTHAFRICA, 'South Africa'),
(MIDDLEEAST, 'Middle-East'),
)
pref_server = models.CharField(
max_length=3,
choices=PREF_SERVER_CHOICES,
default=USWEST,
)
birth_date = models.DateField(null=True, blank=False,)
sessions_played = models.IntegerField(null=False, blank=False, default='0',)
teamwork_commends = models.IntegerField(null=False, blank=False, default='0',)
communication_commends = models.IntegerField(null=False, blank=False, default='0',)
skill_commends = models.IntegerField(null=False, blank=False, default='0',)
positivity_commends = models.IntegerField(null=False, blank=False, default='0',)
FORMS.PY
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from apps.api.models import Profile, Feedback
from django.forms import ModelForm
#form to create profile
#RegistrationForm VIEW must be created first as well as URl
class RegistrationForm(UserCreationForm):
first_name = forms.CharField(max_length=30, required = False, help_text='Optional.')
last_name = forms.CharField(max_length=30, required = False, help_text='Optional.')
email = forms.EmailField(max_length=254, required=True, help_text='Required. Enter a valid email address.')
birth_date = forms.DateField(help_text='Required. Format: YYYY-MM-DD')
#Class meta will dictate what the form uses for its fields
class Meta:
model = User
fields = (
'username',
'first_name',
'last_name',
'email',
'birth_date',
'password1',
'password2',
)
#Function to save form details
def save(self,commit=True):
if commit:
#Setting Commit to false,otherwise It will only save the fields existing in UserCreationForm
user = super(RegistrationForm, self).save(commit=False)
#Adding additional Fields that need to be saved
#Cleaned data prevents SQL Injections
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.email = self.cleaned_data['email']
Profile.birth_date = self.cleaned_data['birth_date']
user.save()
return user
# User editing profile details
class EditProfileForm(RegistrationForm):
class Meta:
model = User
fields = [
'email',
'first_name',
'last_name',
'birth_date',
]
I am creating a view that handles a new Customer registration. My Customer model OneToOne links to Person which again OneToOne links to User where User is AUTH_USER_MODEL. When I send a POST request for customer registration through the register_customer view, person_serialized.is_valid() returns {"user":["This field is required."]} as the user object is not available and passed on the PersonSerializer. I think there should be a better and cleaner to do what I am doing here. Any solutions?
models.py
class User(AbstractBaseUser, PermissionsMixin):
USERNAME_FIELD = 'mobile_number'
# Form validation errors
mobile_number_errors = {'required': 'Mobile number is required',
'invalid': 'Enter a valid 10 digit mobile number' +
'without spaces, + or isd code.'}
_mobile_regex_validator = RegexValidator(regex=r"^\d{10}$",
message="Phone number must be 10 digits without + or spaces.")
mobile_number = models.CharField("Mobile Number", max_length=10,
validators=[_mobile_regex_validator],
blank=False, null=False, unique=True,
error_messages=mobile_number_errors)
is_active = models.BooleanField("Is Active?", default=True)
is_staff = models.BooleanField("Is Staff?", default=False)
created = models.DateTimeField("Account created on", auto_now_add=True, blank=True, null=True)
modified = models.DateTimeField("Last Modified on", auto_now=True, blank=True, null=True)
objects = UserManager()
def __unicode__(self):
return self.mobile_number
def get_full_name(self):
return self.mobile_number
def get_short_name(self):
return self.mobile_number
class Person(models.Model):
user = models.OneToOneField('users.User')
GENDER_CHOICES = (('M', 'Male'),
('F', 'Female'),
('N', 'Not Specified'))
first_name = models.CharField("First Name", max_length=32, blank=False, null=False)
last_name = models.CharField("Last Name", max_length=32, blank=False, null=False)
gender = models.CharField("Gender", max_length=1, choices=GENDER_CHOICES, blank=False, default='N')
class Customer(models.Model):
person = models.OneToOneField('users.Person')
email_errors = {'required': 'Email field is required.',
'invalid': 'Enter a valid email id.'}
email = models.EmailField("Email", blank=False, null=False, unique=True,
error_messages=email_error
serializers.py
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', 'mobile_number',)
class PersonSerializer(serializers.ModelSerializer):
user = UserSerializer()
class Meta:
model = Person
fields = ('id', 'user', 'first_name', 'last_name', 'gender',)
class CustomerSerializer(serializers.ModelSerializer):
person = PersonSerializer()
class Meta:
model = Customer
fields = ('id', 'person', 'email',)
views.py
from django.shortcuts import render
from .serializers import UserSerializer, PersonSerializer, CustomerSerializer
from .models import User, Person, Customer
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
# Create your views here.
#api_view(['POST'])
def register_customer(request):
user_serialized = UserSerializer(data=request.data)
person_serialized = PersonSerializer(data=request.data)
customer_serialized = CustomerSerializer(data=request.data)
if user_serialized.is_valid() and person_serialized.is_valid() and customer_serialized.is_valid():
user = User.objects.create_user(mobile_number=user_serialized.data['mobile_number'],
password=user_serialized.init_data['password'])
person = Person(user=user, first_name=person_serialized.data['first_name'],
last_name=person_serialized.data['last_name'],
gender=person_serialized.data['gender'])
person.save()
customer = Customer(person=person, email=customer_serialized.data['email'])
customer.save()
return Response(request.data, status=status.HTTP_201_CREATED)
else:
return Response(person_serialized._errors, status=status.HTTP_400_BAD_REQUEST)