Reputation: 96
I am working on a Job Portal website and having some questions I wonder what's the correct way of using the relations in my case.
For example:
I have a CompanyProfile where a company can have multiple locations.
I have a Job that can have multiple locations aswell.
I have a Student, and a Student can make a Curriculum Vitae that exists of SkillSet, Education and Experience.
This is wat I have now: A class CompanyProfile A Location Class where a CompanyProfile is a foreignkey of. A Job class where CompanyProfile and Location is foreinkey of. A Skillset, Education and Experience class all 3 of them have foreign key to CV.
CompanyProfile
class CompanyProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, related_name='company_profile')
company_name = models.CharField(max_length=30)
Location
class Location(models.Model):
company = models.ForeignKey(CompanyProfile, on_delete=models.CASCADE)
address = models.CharField(max_length=100, null=True)
zip_code = models.CharField(max_length=10, null=True)
city = models.CharField(max_length=30, null=True)
CV
class Cv(models.Model):
user = models.ForeignKey(StudentProfile, on_delete=models.CASCADE)
Education, Experience and Skillset all same
class Education(models.Model):
cv = models.ForeignKey(Cv, on_delete=models.CASCADE)
...
... more fields.
My question is. Is this the right way or should I Put a foreignkey in CompanyProfile class to Location. And should I put in CV 3 foreign keys of education, experience and skillset or is the code above the right way? I'm stuck on this one, becouse everytime I migrate, I can migrate but sometimes I get something like "No default value for ForeignKey, either give default value NULL or change model".
Upvotes: 0
Views: 38
Reputation: 2436
For the Location and Company part, a Location object can have multiple Company's and a company can have multiple locations. So this is a many-to-many relationship. You can create it outright or even you can define an intermediary model such as Address. You have to define using through
keyword when defining the model
read more about it in here:
https://docs.djangoproject.com/en/2.2/topics/db/examples/many_to_many/
Upvotes: 0
Reputation: 21
your point no.1 is fine, putting a company foreign key in location will work fine in your situation. And for job, a location can have multiple jobs vice versa a job can be in multiple locations. So put a ManyToManyField in Job model for location as below
class Job(models.Model):
locations = models.ManyToManyField(CompanyLocation)
yes you can use foreignkey in skill set education model
No default value for ForeignKey error at makemigrations is something you need to understand, when you define a model first time and migrate there is no problem whether a field is required or not as it had no data in database. but when you add required fields in any model which is already been created in db, you get this error. at makemigrations time django asks to give default values so that it can update in existing rows of that table in db.
Upvotes: 1