1 year ago
#385572
vinoth kumar
Need to populate the Project manager field based on the project select on the Timelog Model using django rest framework
models.py
class User(AbstractBaseUser, PermissionsMixin):
BLOOD_GROUP_CHOICES = (
('a+','A+'),
('a-','A-'),
('b+','B+'),
('b-','B-'),
('ab+','AB+'),
('ab-','AB-'),
('o+','O+'),
('o-','O-')
)
BILLABLE_and_NON_BILLABLE_CHOICES=(
('Billable','Billable'),
('Non-Billable','Non-Billable')
)
username = models.CharField(max_length=30, unique=True,default=None)
email = models.EmailField(max_length=250, unique=True)
first_name = models.CharField(max_length=30, blank=True, null=True)
last_name = models.CharField(max_length=30, blank=True, null=True)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=True)
is_superuser = models.BooleanField(default=False)
date_joined = models.DateTimeField(default=timezone.now)
dob=models.DateField(max_length=8,default=None,null=True, blank=True)
pancard=models.CharField(max_length=25,default=None,null=True, blank=True)
aadhar=models.CharField(max_length=20,default=None,null=True, blank=True)
personal_email_id=models.EmailField(max_length=254,default=None,null=True, blank=True)
phone = PhoneNumberField(default=None,null=True, blank=True)
emergency_contact_no=models.IntegerField(default=None,null=True, blank=True)
emergency_contact_name=models.CharField(max_length=100,null=True, blank=True)
relation=models.CharField(max_length=25,default=None,null=True, blank=True)
blood_group=models.CharField(max_length=25,choices=BLOOD_GROUP_CHOICES,null=True,blank=True)
designation=models.ForeignKey(Designation,on_delete=CASCADE,related_name="designations",default=None,null=True, blank=True)
billable_and_non_billable=models.CharField(max_length=25,choices=BILLABLE_and_NON_BILLABLE_CHOICES,default='Billable',null=True, blank=True)
joining_date=models.DateField(max_length=15,null=True, blank=True)
relieving_date=models.DateField(max_length=15,null=True, blank=True)
is_manager=models.BooleanField(default=False)
reporting_manager = models.ForeignKey('self', null=True, blank=True, on_delete=models.CASCADE)
objects = UserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email', ]
class Project(models.Model):
project_code = models.CharField(primary_key=False, max_length=10,default=None,null=True,unique=True)
project_name = models.CharField(max_length=50,unique=True,default=None)
client= models.ForeignKey(Client,on_delete=CASCADE,related_name="Client1",default=None)
user=models.ManyToManyField(User,related_name='users',default=None)
project_manager = models.ForeignKey(User,on_delete=models.PROTECT,related_name="project_manager",default=None,limit_choices_to = {'is_manager': True},null=True,blank=True)
description=models.TextField()
type=models.TextField() #dropdown
start_date = models.DateTimeField(max_length=10)
end_date=models.DateTimeField(max_length=10)
technical_contact_name = models.CharField(max_length=30)
email=models.EmailField(max_length=254,default=None)
phone = PhoneField(blank=True)
delivery_head_contact_name=models.CharField(max_length=30)
class Meta:
db_table ='Project'
def __str__(self):
if self.client is not None:
return f'{self.client.client_code }{self.project_code}-{self.project_name}'
else:
return self.project_code
class Timelog(models.Model):
STATUS_CHOICES = [
('created','Created'),
('submitted', 'Submitted'),
('approved', 'Approved'),
]
project = models.ForeignKey(Project,on_delete=CASCADE,related_name='project2',default=None)
user= models.ForeignKey(User,on_delete=CASCADE,related_name='user2',default=None,blank=True,null=True)
project_manager = models.ForeignKey(Project,on_delete=CASCADE,related_name='project_manager2',default=None,blank=True,null=True)
job=ChainedForeignKey(Job,chained_field="project", chained_model_field="project",show_all=False, auto_choose=True, sort=True)
date= models.DateField(default = datetime.date.today)
hours=models.DurationField(default=datetime.timedelta(),null=True)
status = models.CharField(max_length=20, choices=STATUS_CHOICES,null=False, default='Created')
def save(self, *args, **kwargs):
if not self.project_manager:
self.project_manager = self.project.project_manager
return super().save(*args, **kwargs)
class Meta:
db_table ='Timelog'
def __str__(self):
return '{}'.format(self.date)
As per my above code automatically i need to get the project managers of that specific project in the timelog model when i select the project and submit the timelog. (for ex: If a user 'Vinoth' is a project Manager and other users are assigned on that project too, and I have a Project assigned to vinoth and the project name is 'API'. If a user enter a timelog they select the project 'API' and date and time, once they submit the request the project manager field needs to populate the project manager for that project automatically)
Can you please help me to complete this, as I tried by above method but it is throwing an error while migrating
"django.db.utils.IntegrityError: insert or update on table "Timelog" violates foreign key constraint "Timelog_project_manager_id_706fe60b_fk_Project_id"
DETAIL: Key (project_manager_id)=(2) is not present in table "Project"."
I dont know how to do this in other ways, kindly help me to fix this issue.
django
django-models
django-rest-framework
django-views
django-admin
0 Answers
Your Answer