0

model #1

class Add_Job(models.Model):
    
    job_name=models.CharField(max_length=254)
    client=models.ForeignKey(Client,on_delete=CASCADE,related_name='client')
    #client=models.ManyToManyField(Client)
    project=models.ForeignKey(Project,on_delete=CASCADE,related_name='project')
    #project=models.ManyToManyField(Project)
    user=models.ForeignKey(Users,on_delete=CASCADE)
    Hours=models.TimeField(null=True)
    start_Date = models.DateTimeField(max_length=10)
    end_Date=models.DateTimeField(max_length=10)
    
    def __str__(self):
        return str(self.id, self.job_name)

model #2

class Add_Timelog(models.Model):
    
    
    project=models.ManyToManyField(Project)
    client=models.ManyToManyField(Client)
    Job=models.ManyToManyField(Add_Job)
    #Date = models.DateField(max_length=100 ,auto_now_add=True,editable=True)
    Date= models.DateField(default = datetime.date.today)
    Hours=models.TimeField(null=True)
    
    def __str__(self):
        return str(self.Date)

while I tried to return the 'id' and 'job_name' from the 'Add_Job' model it is getting reflected in the database table. But the 'Add_timelog' model is getting an error

TypeError at /api/Add_Timelog/ decoding to str: need a bytes-like object, int found

Don't know why it is getting an error.

After update1:

class Add_Timelog(models.Model):
    
    
    project=models.ManyToManyField(Project)
    client=models.ManyToManyField(Client)
    Job=models.ManyToManyField(Add_Job)
    #Date = models.DateField(max_length=100 ,auto_now_add=True,editable=True)
    Date= models.DateField(default = datetime.date.today)
    Hours=models.TimeField(null=True)
    
    def __str__(self):
        return str( '{} {}'.format(self.Date, self.Hours))

model 2

class Consolidated(models.Model):
    
   emp_name=models.ManyToManyField(Users,related_name="employee_name+")
    proj_name=models.ManyToManyField(Project)
    custom_name=models.ManyToManyField(Client)
    Cons_date=models.ManyToManyField(Add_Timelog)
    bill_no_bill=models.ManyToManyField(Users,related_name="billable_and_non_billable+")
    hour_spent = models.ManyToManyField(Add_Timelog,related_name="hour_spent")
    
    def __str__(self):
       return str(self.emp_name)

So as per property method code it is returning the value as object and not as value. But I defined a str function and removed the property method and I am getting the value but both date and hours in the same field. But as per my model "consolidated", I need to get that date seperately and hours seperately. Is that possible to do that in django model ?

vinoth kumar
  • 101
  • 2
  • 11
  • try to change your ``__str__`` return type like this ``return f"{self.Date}"`` – Ankit Tiwari Feb 07 '22 at 05:21
  • I'm new to this django, can you please explain me in detail regarding the return 'f' type. Thanks in advance – vinoth kumar Feb 07 '22 at 05:30
  • Hello @vinothkumar check this [post](https://stackoverflow.com/a/63678773/14457833) to get understanding about ``f""`` – Ankit Tiwari Feb 07 '22 at 05:52
  • @ Ankit Tiwari I tried using this format function, but it doesn't work on the django rest framework. can you please suggest me other ways that could work on the django models. – vinoth kumar Feb 07 '22 at 06:24

1 Answers1

0

It looks like problem with Add_Job model, not Add_Timelog. In str method you are trying to convert id and job_name to str, but this raises TypeError. Try to change return str(self.id, self.job_name) to return '{} {}'.format(str(self.id), self.job_name) it's going to convert id of object to string and put it in first {} and job_name, which is already string in second {}

If you want to access both Date and Hours you can do it using @property method decorator. In your case you would need to write in your Add_Timelog model.

@property
def date_and_hours(self):
    return '{} {}'.format(self.Date, self.Hours)

If you want to get their values not string representation just return two values from property, but you need to unpack them.

@property
def date_and_hours(self):
    return self.Date, self.Hours
PTomasz
  • 1,330
  • 1
  • 7
  • 23
  • Thanks this works. But as a final output we need to return two fields value from the same model seperately( ex: from "Add_Timelog" model we need to return Date and Hours field seperately). Can this be done in django? Thanks in advance.. – vinoth kumar Feb 07 '22 at 09:51
  • What do you mean by return fields seperately? Do you want their string representation or values? – PTomasz Feb 07 '22 at 10:28
  • I am trying to take the date and hours field as a input in another model and display the value on that model while I enter a value in date and hour field here.( Ex: if I enter a date as today and time as now in the Add_Timelog model, I need to get that value in another model say "consolidated" as a seperate field using a foreign key). As of now iam only able to get the date to other model not the hours, but i need it both. kindly help on the same – vinoth kumar Feb 07 '22 at 10:50
  • Edited my answer to add some code there – PTomasz Feb 07 '22 at 11:00
  • I have updated my code after this solution as "after update1" can you please check and help to solve the issue. Thanks in advance – vinoth kumar Feb 08 '22 at 03:46