Hi, Plz look at docs for more info:
https://docs.djangoproject.com/en/2.2/intro/overview/ On Tue, Feb 4, 2020, 09:59 Mike Dewhirst <[email protected]> wrote: > On 4/02/2020 10:41 am, Zameer Ahmed wrote: > > Hi, > > I've also posted this question on StackoverFlow and haven't got any > > response surprisingly. Here is the link > > < > https://stackoverflow.com/questions/60048405/django-extending-models-with-multi-role-users-and-multiple-requirement-types > >. > > > > I am new to Django only 1 week and I am stuck with scenario I need > > help with. > > This is kind of core concept and I apologize in advance for my lack of > > knowledge. > > I've extended the base user model like below with multiple roles. Now > > each role has distinct requirements for their profiles. > > I need help to understand that how to extend Students or Staff > > further. There are two scenario I need to extend them. > > > > 1. Both can have similar extended models like addresses below. > > 2. Both can have different extended models like Students have > > CurrentCourse and Staff does not. Staff has Salary model and Students > > does not. > > > > class User(AbstractUser): > > is_student = models.BooleanField(default=True) > > is_staff = models.BooleanField(default=True) > > is_staff already exists in AbstractUser for purposes of controlling > whether the user may login to the Admin site. If you really need the > concept it may be a good idea to think of a different name for your > field to avoid possible confusion later. > > > class Student(models.Model): > > user = models.OneToOneField(User, on_delete=models.CASCADE, null=True) > > dob = models.CharField(max_length=30, blank=True) > > location = models.CharField(max_length=30, blank=True) > > Having such a relationship makes User.is_student redundant. I wouldn't > have is_student at all. Likewise is_staff. > > > class CurrentCourse(models.Model): > > student = models.OneToOneField(Student, on_delete=model.CASCADE)# Can > > I extend it like this or do i need some other approach? > > Not sure what you mean by CurrentCourse. If there are many students and > many courses you probably want a ManyToManyField between (I presume) > Course and User. The name CurrentCourse indicates to me that a student > can be doing only one course (of many) at a time. Without knowing your > intentions it is difficult to say much more. > > > > .... > > > > class Staff(models.Model): > > ROLES = (('Teacher', 'Teacher'), ('Professor', 'Professor'), > > ('Administration', 'Administration')) > > You have omitted 'Tutor' and that might merge student and staff. > However, you have the option to connect both Student and Staff to User > so you retain flexibility to let students also join the staff. > > > user = models.OneToOneField(User, on_delete=models.CASCADE, null=True) > > role = models.CharField(max_length=100, choices=ROLES) > > website = models.CharField(max_length=100, blank=True) > > > > # Both types of Users can have multiple addresses and both Students > > and Staff needs addressess, not sure how to extend this with ForeignKey. > > Just think about the real world. Do people share addresses? Do people > have multiple addresses? Are students and staff both Users? > > Students and staff are both users so on the Address model you might want > a ManyToManyField pointing to User. > > I quite like ManyToManyField because the through table which is > automatically created can be adjusted to contain additional information > which describes the relationship. For example, a student might have a > local residential and a separate postal address as well as a more > distant parental address. The enhanced through table would be useful for > differentiating between them. > > If a User can have only one address then you need a ForeignKey on User > which points to an address. > > > > > class Address(models.Model): > > street = models.CharField(max_length=200) > > city = models.CharField(max_length=50) > > country = models.CharField(max_length=50) > > > > class Salary(models.Model): > > staff = models.OneToOneField(Staff, on_delete=models.CASCADE) > > current_salary = models.DecimalField(max_digits=10, decimal_places=2) > > Finally please let me know how can I apply validators on each model > > for instance I did sub-classed 'User' to all models instead of Student > > or Staff. How to apply a validator on OneToOneField like below: > > > > class Salary(models.Model): > > staff = models.OneToOneField(User, on_delete=models.CASCADE, > > validators=[some-validator]) > > > > > > Thank you in advance for your kind help for this. > > > > Best Regards, > > > > Zaheer > > -- > > 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 [email protected] > > <mailto:[email protected]>. > > To view this discussion on the web visit > > > https://groups.google.com/d/msgid/django-users/51552bea-877b-4c66-8a7a-42a68ca761e5%40googlegroups.com > > < > https://groups.google.com/d/msgid/django-users/51552bea-877b-4c66-8a7a-42a68ca761e5%40googlegroups.com?utm_medium=email&utm_source=footer > >. > > -- > 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 [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/84e69a79-d0bc-7fe4-8a6b-e757352c27ee%40dewhirst.com.au > . > -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAP5HUWqkk9-gH3Vg4g6A%2BW_jY4p228kz7zpv2FFHzMyFwWk2KQ%40mail.gmail.com.

