Two types of users, how would I authenticate them?

139 views
Skip to first unread message

charles javelona

unread,
May 9, 2015, 12:27:06 PM5/9/15
to django...@googlegroups.com
Hi there, 

I am a newbie in Django. My goal is to create an online platform exchange for my community that helps students like me get part-time jobs from employers.
For the past days, I taught myself how to build a blog and the polls app, but it seems what I am building is more complex than a regular tutorial.

My first few obstacles are the following:
  - Registering and authenticating two types of users, Employers and Students.
  - Logging two the two types of users.
  - How would I even start defining the models for this?


Here is my first attempt of defining them.
from django.db import models

class User(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
email = models.EmailField(max_length=254, unique=True)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
mobile = models.CharField(max_length=12)
password = models.CharField(max_length=100, null=True)

def __str__(self):
return self.email

class Meta:
abstract = True


class Student(User):
school = models.CharField(max_length=40)


class Employer(User):
company = models.CharField(max_length=40)


Can someone please guide me in the right direction? I am fairly confuse and I do not want to abstract too much of the code because I want to truly understand how the Django architecure works.
If you need more information, feel free to ask me and I would be happy to clarify things.

Thanks a bunch!
Charles

Tom Evans

unread,
May 9, 2015, 2:52:30 PM5/9/15
to django...@googlegroups.com
I would simplify things, have just one type of user, who can be
associated with 1:N companies and 1:N schools. A user is an employer
if they are associated with at least one company, and a user is a
student if they are associated with at least one school.

You could denormalise the relationship with attributes on the user
model if/when you are worried about performance.

Eg:

class Company(models.Model):
name = ...
contact_details = ...

class School(models.Model):
name = ....

class User(models.Model):
...
company = models.ManyToManyField(Company)
school = models.ManytoManyField(School)


You could expand things about the relationship by adding a information
to the join (or "through") table; perhaps you might want to record
when a student started/left a school; this would also allow you to
record which employers are alumni of a school:

https://docs.djangoproject.com/en/1.8/topics/db/models/#intermediary-manytomany

Cheers

Tom

Tim Chase

unread,
May 9, 2015, 3:15:52 PM5/9/15
to django...@googlegroups.com
On 2015-05-09 15:52, Tom Evans wrote:
> I would simplify things, have just one type of user, who can be
> associated with 1:N companies and 1:N schools. A user is an employer
> if they are associated with at least one company, and a user is a
> student if they are associated with at least one school.
>
> You could denormalise the relationship with attributes on the user
> model if/when you are worried about performance.
>
> Eg:
>
> class Company(models.Model):
> name = ...
> contact_details = ...
>
> class School(models.Model):
> name = ....
>
> class User(models.Model):
> ...
> company = models.ManyToManyField(Company)
> school = models.ManytoManyField(School)

Since people can attend multiple schools and work for multiple
employers, you can just make it a many-to-many, using the stock
auth User:

from django.contrib.auth.models import User

class Company(models.Model):
...

class School(models.Model):
...

class Employee(models.Model):
company = models.ForeignKey(Company)
employee = models.ForeignKey(User)

class Student(models.Model):
school = models.ForeignKey(School)
employee = models.ForeignKey(User)

-tkc



Tom Evans

unread,
May 9, 2015, 6:18:28 PM5/9/15
to django...@googlegroups.com
On Sat, May 9, 2015 at 4:15 PM, Tim Chase
<django...@tim.thechases.com> wrote:
> Since people can attend multiple schools and work for multiple
> employers, you can just make it a many-to-many, using the stock
> auth User:
>
> from django.contrib.auth.models import User
>
> class Company(models.Model):
> ...
>
> class School(models.Model):
> ...
>
> class Employee(models.Model):
> company = models.ForeignKey(Company)
> employee = models.ForeignKey(User)
>
> class Student(models.Model):
> school = models.ForeignKey(School)
> employee = models.ForeignKey(User)
>

This would just be explicitly defining the join or through tables used
for a M2M relationship, and is unnecessary if you are not adding
fields to the join table - Django can infer those tables for itself.

If you aren't adding an extra information to the join table, it would
still be better to specify them as a ManyToManyField, so that django
can provide a few extra bits of magic.

Django does not care what model you place the M2M relation on, so
without modifying the stock User model...

class Company(models.Model):
employees = models.ManyToManyField(User)

class School(models.Model):
students = models.ManyToManyField(User)


If you then wanted to define extra attributes on the relationship, eg
for a student, date started or date left...

class Student(models.Model):
school = models.ForeignKey(School)
user = models.ForeignKey(User)
enrolled_date = models.DateField(blank=True, null=True)
graduation_date = models.DateField(blank=True, null=True)

class School(models.Model):
students = models.ManyToManyField(User, through=Student)

etc

It is always worth defining that a join table is part of a M2M
relationship, eg with a School and a User, you could do:

some_school.students.add(some_user)

Without defining it as a M2M relationship, you would have to say:

Student.objects.create(school=some_school, user=some_user)

which not only is less clear, but is also more typing!

Cheers

Tom
Reply all
Reply to author
Forward
0 new messages