Reputation: 167
I'm sort of new to Django and am currently struggling with a certain aspect of it. In my project I want all Users to have access to certain Job
s added by the admin and appear on the Front end. However each Job
has a field called userStatus
that the user should be able to edit. This field however should not change for any other users.
Currently I have created a model out of userStatus
and created a Foreign Key field to link to a Job
. Then userStatus
has a User
Foreign Key Field.
class Job(models.Model):
name = models.CharField(max_length=200, help_text='Enter the Spring/Internship')
course = models.ForeignKey('Course', on_delete=models.RESTRICT,null=True)
company = models.ForeignKey('Company', on_delete=models.RESTRICT,default='N/A')
deadline = models.DateField(null=True, blank=True)
userStatus = models.ForeignKey('userStatus', on_delete=models.RESTRICT, null=True, blank=True)
class userStatus(models.Model):
user = models.ForeignKey(User, on_delete=models.RESTRICT)
USER_STATUS = (
('n', 'Not Applied'),
('a', 'Applied'),
('i', 'Got Interview'),
('o', 'Got Offer'),
('r', 'Rejected'),
)
stage = models.CharField(
primary_key=True,
max_length=1,
choices=USER_STATUS,
blank=False,
default='c',
help_text='What is the current stage of the process',
)
The User should not be able to edit any field except for the userStatus
that should only belong to them. The tuple USER_STATUS
should be the only options for that model and every Job
should have access to these 5 options. In context:
One job might be called "Internship at Google", one user may have "Got Interview" to this Job but another user has "Not applied". This Second user may apply and wants to change their status to "Applied". If they do, it should not affect the first user's "Got Interview" status.
I feel as though my approach is completely wrong so please let me know how and what my approach should be.
Upvotes: 0
Views: 36
Reputation: 196
You defined userStatus
in a Job
model, so one specific Job
can have zero or one userStatus
and one userStatus
can have many Jobs
.
Relation between these two models should work in opposite direction.
So the code should be as follows:
class Job(models.Model):
name = models.CharField(max_length=200, help_text='Enter the Spring/Internship')
course = models.ForeignKey('Course', on_delete=models.RESTRICT,null=True)
company = models.ForeignKey('Company', on_delete=models.RESTRICT,default='N/A')
deadline = models.DateField(null=True, blank=True)
class UserStatus(models.Model):
user = models.ForeignKey(User, on_delete=models.RESTRICT)
job = models.ForeignKey(Job, on_delete=models.RESTRICT)
USER_STATUS = (
('n', 'Not Applied'),
('a', 'Applied'),
('i', 'Got Interview'),
('o', 'Got Offer'),
('r', 'Rejected'),
)
stage = models.CharField(
primary_key=True,
max_length=1,
choices=USER_STATUS,
blank=False,
default='c',
help_text='What is the current stage of the process',
)
What can and cannot be edited should be defined in views and serializers.
Upvotes: 1