Wednesday, March 28, 2018

I am trying to create a django project with a custom model that has two diffrent user profiles.it seems to have set a default profile i want to be able to choose one profile from a user

from django.db import models
from django.contrib.auth.models import AbstractUser,UserManager
from django.db.models.signals import post_save
from django.dispatch import receiver

# Create your models here.

class CustomUserManager(UserManager):
pass

class CustomUser(AbstractUser):
pass

class LecProfile(models.Model):
LECTURER = 1
MANAGEMENT = 2
ROLE_CHOICES = (
(LECTURER, 'lecturer'),
(MANAGEMENT, 'management'),
)
role= models.PositiveSmallIntegerField(choices=ROLE_CHOICES,null=True,blank=True)
user = models.OneToOneField(CustomUser,on_delete=models.CASCADE,null=True, related_name='lec_profile')
Employee_no = models.CharField(max_length=100)
Department = models.CharField(max_length=100)
Unit = models.CharField(max_length=100)
lec_image = models.FileField(upload_to='profile_image',blank=True)

def __str__(self):
return self.user.username

@receiver(post_save, sender=CustomUser)
def create_or_update_lec_profile(sender, instance, created, **kwargs):
if created:
LecProfile.objects.create(user=instance)
instance.lec_profile.save()

class UserProfile(models.Model):
STUDENT = 1
STUDENT_REP = 2
ROLE_CHOICES = (
(STUDENT, 'student'),
(STUDENT_REP, 'student_rep'),
)
role= models.PositiveSmallIntegerField(choices=ROLE_CHOICES,null=True,blank=True)
user = models.OneToOneField(CustomUser,on_delete=models.CASCADE,null=True, related_name='user_profile')
reg_no = models.CharField(max_length=100)
year = models.CharField(max_length=100)
course = models.CharField(max_length=100)
your_image = models.FileField(upload_to='profile_image1',blank=True)

def __str__(self):
return self.user.username

@receiver(post_save, sender=CustomUser)
def create_or_update_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
instance.user_profile.save()

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/adb5ddb8-1ea3-4bcc-89d9-51e8174a4a9f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment