0

What is the best way to design a multiple level user login system using django and postgresql. The user details are head(Admin), students, teachers,staffs etc. These different types of user details have different fields and we can't alter that fields. How we design user model by combining all these type of users.

class Heads(models.Model):
    gid     = models.IntegerField(primary_key=True) 
    name    = models.CharField(max_length=50)
    emp_code= models.CharField(max_length=50)
    school  = models.ForeignKey(SchoolDetails)
    prdFrom = models.DateField()
    contact_no=models.CharField(max_length=50) 
    email_id=models.CharField(max_length=50) 
    designation=models.CharField(max_length=50)
    address =models.TextField() 
    def __unicode__(self):
        return unicode(self.name)
    class Meta:
        db_table = u'heads'
        verbose_name = "heads"  

class Student(models.Model):
    gid     = models.IntegerField(primary_key=True) 
    name    = models.CharField(max_length=50)
    stud_code= models.CharField(max_length=50)
    school  = models.ForeignKey(SchoolDetails)
    std = models.IntegerField()
    division=models.CharField()
    parents_email_id=models.CharField(max_length=50) 
    parents_contact_no=models.CharField(max_length=50) 
    addess  =models.TextField() 
    def __unicode__(self):
        return unicode(self.name)
    class Meta:
        db_table = u'students'
        verbose_name = "students"       

class Teacher(models.Model):
    gid     = models.IntegerField(primary_key=True) 
    name    = models.CharField(max_length=50)
    emp_code= models.CharField(max_length=50)
    school  = models.ForeignKey(SchoolDetails) 
    contact_no=models.CharField(max_length=50) 
    email_id=models.CharField(max_length=50) 
    address =models.TextField() 
    is_lead= models.CharField()
    def __unicode__(self):
        return unicode(self.name)
    class Meta:
        db_table = u'teacher'
        verbose_name = "teacher"        

class Staff(models.Model):
    gid     = models.IntegerField(primary_key=True) 
    name    = models.CharField(max_length=50)
    emp_code= models.CharField(max_length=50)
    school  = models.ForeignKey(SchoolDetails) 
    contact_no=models.CharField(max_length=50) 
    email_id=models.CharField(max_length=50) 
    address =models.TextField() 
    designation= models.CharField()
    def __unicode__(self):
        return unicode(self.name)
    class Meta:
        db_table = u'staff'
        verbose_name = "staff"              

Please give answers.

Thank You.

Anju
  • 137
  • 2
  • 10

1 Answers1

0

You could extend the User model with all of your profile-like classes, so i.e. this would give you:

class Heads(models.Model): #<-- by convention you should use singular 'Head'
    user = models.OneToOneField(User)
    <other Head-specific fields not included in User>

So the User-type is always the same, but the attached profile is different. If you use the OneToOneField as in the example, on the backwards-relation you would only have to check which one is not None to come from a User-instance to the correct Profile-instance.

Of course, you probably still would have different forms or methods for User creation depending on what profile the user belongs to.

If you don't feel comfortable with the information Django's default User model holds, you could also go a step further and substitute the User model to hold all the information that is common to all profiles.

ascripter
  • 5,665
  • 12
  • 45
  • 68