I am trying to create a signal to create a profile for a newly created user. However, I need to have two different types of profiles: A customer profile and an employee profile. The type of profile which will be created is decided inside my User model via the "user_type" field:
user/models.py:
class User(AbstractBaseUser):
USER_TYPES = (
('Employee', 'employee'),
('Customer', 'customer'),
('Vendor', 'vendor')
)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
phone_number = models.CharField(max_length=20)
user_type = models.CharField(max_length=8, choices=USER_TYPES)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = MyUserManager()
USERNAME_FIELD = 'email'
def __str__(self):
return f'{self.first_name} {self.last_name} : {self.email}'
def has_perm(self, perm, obj=None):
"Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
#property
def is_staff(self):
"Is the user a member of staff?"
# Simplest possible answer: All admins are staff
return self.is_admin
# if user.is_employee == True
class EmployeeProfile(models.Model):
EMPLOYEE_ROLES = (
('Driver', 'driver'),
('Production', 'production'),
('Manager', 'manger')
)
user = models.OneToOneField(User, on_delete=models.CASCADE)
role = models.CharField(max_length=12, choices=EMPLOYEE_ROLES)
def __str__(self):
return str(self.user)
# if user.is_customer == True
class CustomerProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
company = models.CharField(max_length=100, null=True)
address = models.CharField(max_length=100, null=True)
address_2 = models.CharField(max_length=100, null=True)
city = models.CharField(max_length=50, null=True)
state = models.CharField(max_length=2, help_text="State Abbreviation (ex: OH)", null=True)
zipcode = models.CharField(max_length=5, null=True)
def __str__(self):
return str(self.user)
and then in my user/signals.py file:
#receiver(post_save, sender=User)
def create_customer_profile(sender, instance, created, **kwargs):
if created:
if User.user_type == 'customer':
CustomerProfile.objects.create(user=instance)
#receiver(post_save, sender=User)
def create_employee_profile(sender, instance, created, **kwargs):
if created:
if User.user_type == 'employee':
EmployeeProfile.objects.create(user=instance)
#receiver(post_save, sender=User, dispatch_uid='save_new_user_profile')
def save_profile(sender, instance, created, **kwargs):
user = instance
if created:
if User.user_type == 'customer':
customer_profile = CustomerProfile(user=user)
customer_profile.save()
if created:
if User.user_type == 'employee':
employee_profile = EmployeeProfile(user=user)
employee_profile.save()
When I create a user, no profiles are created. I was able to get a profile created by just using one Profile model and the standard signal:
#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()
... but ideally I want to have two separate profiles depending on the type of user that is created.
You can do this by overriding the save method from the User model.
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
if self.user_type == 'customer':
CustomerProfile.objects.create(user=self)
elif self.user_type == 'employee':
EmployeeProfile.objects.create(user=self)
Related
I trying to update the user profile but I can't get that what mistake I do here.
#forms.py
class AddStudentUserForm(forms.ModelForm):
class Meta:
model = User
fields = ["username", "first_name", "last_name"]
class AddStudentStaffForm(forms.ModelForm):
class Meta:
model = Staff
fields = ["gender", "dob", "mob", "img"]
#views.py
def editProfile(request, user, id):
if request.POST:
staffform = AddStudentStaffForm(request.POST or None, request.FILES or None, instance=request.user.staff_profile)
userform = AddStudentUserForm(data=request.POST, instance=request.user)
if staffform.is_valid():
staffform.save()
if userform.is_valid():
userform.save()
return redirect(profile,user)
return render(request, 'edit.html')
#models.py
class Staff(models.Model):
Male = 'Male'
Female = 'Female'
Other = 'Other'
GENDER_CHOICE = (
(Male, 'Male'),
(Female, 'Female'),
(Other, 'Other')
)
id = models.AutoField(primary_key=True)
admin = models.OneToOneField(User, related_name='staff_profile', on_delete=models.CASCADE)
gender = models.CharField(max_length=10, choices=GENDER_CHOICE, default=Male)
dob = models.DateField(auto_now_add=False, blank=True, null=True, default=None)
mob = models.IntegerField(blank=True, null=True, default=None)
img = models.ImageField(upload_to='staff/', blank="True", null="True")
objects = models.Manager()
#receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Staff.objects.create(user=instance)
#receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.staff_profile.save()
def __str__(self):
return self.gender
I started to create a Student Management System project. But I faced some difficulties to develop it. I trying lot of to solve these problem but since I can't solve that.
Here if I want to update the Staff model fields like gender, dob, mob it will updated successfully but when I trying to update the User model fields like username, first_name, last_name it will not update. I think the if userform.is_valid(): condition couldn't run, but I can't get what mistake I do here.
You can debug why the form is not valid, try code below :
if userform.is_valid():
userform.save()
return redirect(profile,user)
else:
print userform.errors #To see the form errors in the console.
I am trying to update the user profile in django rest framework
So i am able to create the user profile simultaneously with user creation using signals
Now i am trying to update the profile created:-
//models.py
class User(AbstractBaseUser,PermissionsMixin):
phone_regex=RegexValidator(regex = r'^[6-9]\d{9}$',message='please enter the correct phonenumber')
#name_regex=RegexValidator(regex=r'/^[A-Za-z]+$/',message='Please enter the correct name')
phone=models.CharField(validators=[phone_regex],max_length=15,unique=True)
date_joined=models.DateTimeField(verbose_name='date joined',auto_now_add=True)
last_login=models.DateTimeField(verbose_name='last login',auto_now=True)
active = models.BooleanField(default=True)
staff = models.BooleanField(default=False) # a admin user; non super-user
admin = models.BooleanField(default=False)
first_login=models.BooleanField(default=True)
USERNAME_FIELD='phone'
REQUIRED_FIELDS = []
class UserProfile(models.Model):
# There is an inherent relationship between the Profile and
# User models. By creating a one-to-one relationship between the two, we
# are formalizing this relationship. Every user will have one -- and only
# one -- related Profile model.
GENDER = (
('M', 'Homme'),
('F', 'Femme'),
)
user = models.OneToOneField(
User,related_name="profile", on_delete=models.CASCADE
)
# Each user profile will have a field where they can tell other users
# something about themselves. This field will be empty when the user
# creates their account, so we specify blank=True.
email=models.EmailField(unique=True,validators=[validate_email],max_length=254,blank=True,null=True)
name=models.CharField(max_length=15,blank=True)
dateofbirth=models.DateField(auto_now=False, null=True, blank=True)
Gender=models.CharField(max_length=1, choices=GENDER,blank=True,null=True)
address1 = models.CharField(
"Address",
max_length=1024,
blank=True,
null=True
)
address2 = models.CharField(
"Society",
max_length=1024,
blank=True,
null=True
)
address3 = models.CharField(
"Landmark",
max_length=1024,
blank=True,
null=True
)
zip_code = models.CharField(
"ZIP / Postal code",
max_length=12,
blank=True,
null=True
)
city = models.CharField(
"City",
max_length=1024,
default ="Gurugram",
blank=True,
null=True
)
country = models.CharField(
"Country",
max_length=10,
default="India",
blank=True,
null=True
)
# In addition to the `bio` field, each user may have a profile image or
# avatar. This field is not required and it may be blank.
# A timestamp representing when this object was created.
created_at = models.DateTimeField(auto_now_add=True,blank=True)
# A timestamp representing when this object was last updated.
updated_at = models.DateTimeField(auto_now=True,blank=True)
def __str__(self):
return self.user.phone
#receiver(post_save, sender=User)
def create_profile_for_user(sender, instance=None, created=False, **kargs):
if created:
UserProfile.objects.get_or_create(user=instance)
// Serializer.py
class UserProfileSerializer(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields=("name",)
class UserSerializer(serializers.ModelSerializer):
user = UserProfileSerializer(many=True)
class Meta:
model = User
fields = ('user', 'phone',)
def create(self, validated_data):
user_data = validated_data.pop('user')
user = User.objects.create(**user_data)
profile = user_data.objects.create(user=user, **validated_data)
return profile
def update(self, instance, validated_data):
print("update function")
# profile_data = validated_data.pop('profile')
# print(profile_data)
profile_data = validated_data.pop('user')
profile = instance.profile
# print("profile is ",profile)
print(profile_data.name)
print("name in validation ",validated_data['name'], " type is ",print(type(validated_data)))
instance.username = validated_data.get('name', instance.username)
instance.email = validated_data.get('email', instance.email)
instance.save()
print("name is ",instance.profile.name )
#profile.Gender = validated_data['Gender'],
#profile.email = validated_data['email'],
#profile.dateofbirth=validated_data['dateofbirth'],
#profile.address1=validated_data['address1'],
##profile.address2=validated_data['address2'],
#profile.address3=validated_data['address3']
# print("1",profile)
#print(profile.Gender)
#print(profile.email)
#print(profile.dateofbirth)
profile.save()
print("2",profile)
print(profile.name)
print("save done")
return instance
//views.py
class ProfileCreateAPIView(CreateAPIView):
def put(self, request, *args, **kwargs):
print("my print 0",request)
header_token = request.META.get('HTTP_AUTHORIZATION', None)
print(header_token)
access_token=header_token.split(' ')[1]
status,user =validate_token(access_token)
print(request.data)
user_profile_serializer = UserProfileSerializer(
instance=user,
data=request.data
)
user_serializer = UserSerializer(
instance=user,
data=request.data)
print("my print 1",user_profile_serializer.is_valid())
print("my print 2",user_serializer.is_valid())
if user_profile_serializer.is_valid() and user_serializer.is_valid():
user_profile_serializer.save()
print("user_profile save done")
user_serializer.save()
print("user save done")
return Response(
status=rt_status.HTTP_200_OK)
# Combine errors from both serializers.
# errors = dict()
# errors.update(user_profile_serializer.errors)
#errors.update(user_serializer.errors)
else:
return Response(status=rt_status.HTTP_400_BAD_REQUEST)
'''
return Response(status=rt_status.HTTP_400_BAD_REQUEST)
'''
Now the issue is when i am hitting the api I am getting user_serializer.is_valid() as TRUE but False from UserProfile.
I feel there is some issue with my getting the profile data.
But I am not able to understand how to resolve the issue .
Any help is much appreciated.
I am trying to update UserDetailsSerializer and the problem is when I run my code in my test model it works but when I use it in my actual app, it throws this error: 'User' object has no attribute 'userprofile'
model.py
class userProfileModel(models.Model):
GENDER = [
('', ""),
('M', "Male"),
('F', "Female")
]
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='userprofile', default='')
avatar = models.ImageField(upload_to='avatar/', default='avatar/no-avatar.png')
age = models.DateField(auto_now_add=True)
gender = models.CharField(max_length=10, choices=GENDER, default='')
address = models.CharField(max_length=500, default='')
longitude = models.FloatField(default=0.0)
latitude = models.FloatField(default=0.0)
phone = models.IntegerField(default=0)
user_is_active = models.BooleanField(default=False)
def __str__(self):
return self.user.username
serializers.py
class UserProfileSerializer(serializers.ModelSerializer):
class Meta:
model = userProfileModel
fields = (
'id',
'avatar',
'age',
'gender',
'address',
'longitude',
'latitude',
'phone',
)
class UserDetailsSerializer(UserDetailsSerializer):
profile = UserProfileSerializer(source='userprofile')
class Meta(UserDetailsSerializer.Meta):
fields = UserDetailsSerializer.Meta.fields + ('profile',)
read_only_fields = ('',)
def update(self, instance, validated_data):
# User data
nested_serializer = self.fields['profile']
nested_instance = instance.userprofile
nested_data = validated_data.pop('userprofile')
nested_serializer.update(nested_instance, nested_data)
return super(UserDetailsSerializer, self).update(instance, validated_data)
The error:
RelatedObjectDoesNotExist at /rest-auth/user/
User has no userprofile.
It's because UserProfile instance for user is not created yet. You can use a signal on post_save of User model, so that whenever user is saved, check if UserProfile instance for it is created, if not then create one. like below:
def create_profile(sender,**kwargs ):
if kwargs['created']:
user_profile=UserProfile.objects.create(user=kwargs['instance'])
post_save.connect(create_profile,sender=User)
Also like my friend suggested in comments, Always check if userProfile instance is exist with hasattr(instance, 'profile'), if not then create a default one for that user.
Reference: This is a cool topic Extending user model
When you are creating user profile, make sure you save them, you can refer this snippet.
class Profile(models.Model):
# ...
#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()
And also don't forget to import post_save.
I'm building a fake social network for a project in Django. I've implemented allowing users to 'follow' others if desired (or none at all if they choose). But so far every time I create a user, that user automatically 'follows' all other users in the database. I do not know how to fix this.
Here is the model:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
bio = models.CharField(max_length=200, blank=True)
followers = models.ManyToManyField('Profile',
related_name="followers_profile",
blank=True, symmetrical=False)
following = models.ManyToManyField('Profile',
related_name="following_profile",
blank=True, symmetrical=False)
avatar_thumbnail = ProcessedImageField(upload_to='profile_images',
processors=[ResizeToFill(320, 320)],
format='JPEG',
options={'quality': 40},
blank=True)
feed_type = models.CharField(max_length=8, choices=GENDER_CHOICES,
default='Both')
# #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()
def get_number_of_followers(self):
if self.followers.count():
return self.followers.count()
else:
return 0
def get_number_of_following(self):
if self.following.count():
return self.following.count()
else:
return 0
def __str__(self):
return self.user.username
Here is the View that constructs User and Profiles and the form that processes user creation data:
def signup(request):
form = UserCreateForm()
if request.method == 'POST':
form = UserCreateForm(request.POST)
if form.is_valid():
form.save()
user = User.objects.get(username=request.POST['username'])
user_profile = Profile(user=user)
user_profile.save()
new_user = authenticate(username=form.cleaned_data['username'],
password=form.cleaned_data['password1'])
login(request,new_user)
return redirect('index')
return render(request, 'userapp/signup.html', {
'form': form
})
Here's the UserCreationForm:
class UserCreateForm(UserCreationForm):
email = forms.EmailField(required=True)
class Meta:
model = User
fields = {"username", "email", "password1","password2"}
def save(self, commit=True):
user = super(UserCreateForm, self).save(commit=False)
user.email =self.cleaned_data["email"]
if commit:
user.save()
return user
The scenario is that I'm building an app for users to submit payment and gain or renew "Member" status to the site. If an anonymous person wants to create an account all that's required is an email and a password but for membership we're going to want the User.firstname and User.lastname to be filled in as well. I'm not sure how best to incorporate this requirement in the "enrolment" form. Any guidance on approach or best practices welcome.
Here is the code I'm working with
class MembershipType(models.Model):
"""The represents the type of membership that may be offered."""
active = models.BooleanField(default=True)
long_name = models.CharField(max_length=255)
period = models.PositiveIntegerField() # The number of days the membership is good for
accounts = models.PositiveSmallIntegerField() # The number of accounts that it supports
price = models.DecimalField(max_digits=5,
decimal_places=2)
def __unicode__(self):
return self.long_name
class Membership(models.Model):
"""Represents an active membership of a user. Both the start_date and
end_date parameters are inclusive."""
DEFAULT_DURATION = 365 # the default number of days a membership is active
start_date = models.DateField(auto_created=True)
end_date = models.DateField(null=True)
membership_type = models.ForeignKey(MembershipType)
referral = models.ForeignKey(User, related_name='membership_referral')
# Contact Info
phone = PhoneNumberField()
# Address Fields
address_1 = models.CharField(max_length=255)
address_2 = models.CharField(max_length=255, blank=True)
city = models.CharField(max_length=64)
state = USStateField()
zip_code = USPostalCodeField()
#property
def is_active(self):
return self.end_date >= datetime.date.today()
class MembershipUser(models.Model):
"""Simple relationship that associates a single user to a membership object"""
user = models.OneToOneField(User)
membership = models.ForeignKey(Membership)
def save(self, force_insert=False, force_update=False, using=None,
update_fields=None):
if (self.membership.membership_type.accounts ==
MembershipUser.objects.filter(membership=self.membership).count()):
raise NotImplementedError("The Membership %s, already has it's "
"maximum number of accounts associated "
"with it." % self.membership.id)
Also, as you may see from that code I have to support to idea of a single user purchasing membership that may be used for for than one user. A use case here is a "Couple's" membership.
The way I implemented it is this. It seems to handle everything jsut fine and will prepoluate the user's info. I feel like I should be able to reference the user object and not have to pass it back in but it works.
models.py
class Membership(models.Model):
"""Represents an active membership of a user. Both the start_date and
end_date parameters are inclusive."""
DEFAULT_DURATION = 365 # the default number of days a membership is active
start_date = models.DateField(auto_created=True)
end_date = models.DateField(null=True)
membership_type = models.ForeignKey(MembershipType)
referral = models.ForeignKey(User, related_name='membership_referral', null=True)
# Contact Info
phone = PhoneNumberField()
# Address Fields
address_1 = models.CharField(max_length=255)
address_2 = models.CharField(max_length=255, blank=True)
city = models.CharField(max_length=64)
state = USStateField()
zip_code = USPostalCodeField()
def save(self, force_insert=False, force_update=False, using=None,
update_fields=None):
"""Overload the save function to set the start and end date."""
self.start_date = datetime.date.today()
self.end_date = (self.start_date +
datetime.timedelta(days=self.membership_type.period))
super().save()
#property
def is_active(self):
return self.end_date >= datetime.date.today()
forms.py
class MembershipForm(ModelForm):
"""The Form shown to users when enrolling or renewing for membership."""
def __init__(self, *args, **kwargs):
self.user = kwargs.pop("user", None)
_fields = ('first_name', 'last_name', 'email',)
_initial = model_to_dict(self.user, _fields) if self.user is not None else {}
super(MembershipForm, self).__init__(initial=_initial, *args, **kwargs)
self.fields.update(fields_for_model(User, _fields))
self.fields['referral'].required = False
class Meta:
model = Membership
fields = ['membership_type', 'referral', 'phone', 'address_1',
'address_2', 'city', 'state']
zip_code = USZipCodeField(max_length=5, required=True)
def save(self, *args, **kwargs):
self.user.first_name = self.cleaned_data['first_name']
self.user.last_name = self.cleaned_data['last_name']
self.user.email = self.cleaned_data['email']
self.user.save()
profile = super(MembershipForm, self).save(*args, **kwargs)
return profile
views.py
#login_required
def enroll(request):
template_name = 'enroll.html'
if request.method == 'POST':
form = MembershipForm(request.POST, user=request.user)
if form.is_valid():
form.save()
return redirect('/')
else:
form = MembershipForm(user=request.user)
return render(request, template_name, {'form': form})