Search code examples
djangodjango-users

How to implement User system for job board (I need employers to be able to register and create posts)?


I am creating a job board where users can search a zip code and see jobs in their area. I have the following models.py:

class Business(models.Model):
    name = models.CharField(max_length = 150)
    address = models.CharField(max_length = 150)
    city = models.CharField(max_length = 150)
    zip_code = models.CharField(max_length = 10)
    state = models.CharField(max_length = 30)
    phone_number = models.CharField(max_length = 10)


class Job(models.Model):
    business = models.ForeignKey(Business, on_delete = "models.CASCADE") #when the referenced object is deleted, also delete the objects that have references to it.
    title = models.CharField(max_length = 100)
    description = models.CharField(max_length = 500)
    zip_code = models.CharField(max_length = 10)

    def save(self, *args, **kwargs):
        zip_code = self.business.zip_code
        super(Job, self).save(*args, **kwargs)

To use the website to look for jobs, you do not have to sign in. However, I obviously need employer's to be able to create an account so that they can register a business and post jobs for said business. I am not sure how to approach this and have seen many different ways to go about it online. Should I have the Business model extend the User model as such:

class Business(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    #other fields...

and then when an employer registers show a form that includes the User fields as well as the Business fields?


Solution

  • If your app is only to do this and you're just starting, I would say easiest would be just extending the User model as pointed out in the django docs.

    But most probably you rather need an Employee model (for you may have other types of users!?). Such an Employee model could have a one-to-one relation with the User model for example.

    Here is an overview of the options for extending the User model for different needs.

    p.s. You need not store the zip-code in the Job model (nor override save for that matter). You can access it via the Foreign Key relation. So if job is a Job object: job.business.zip_code will get you the zip_code