Issue
I want to find the therapist that have visited the patient. my visit model:
class visit(models.Model):
customer_name=models.ForeignKey(customerprofile, on_delete=models.CASCADE)
therapist_name=models.ForeignKey(therapistProfiles, on_delete=models.CASCADE, default=1)
The first query works fine but it cannot find the matching query for the second one:
@property
def getthefee(self):
customer = customerprofile.objects.get(c_name=self.customer_name)
therapist=therapistProfiles.objects.get(name=self.therapist_name)
So I used this filtering:
therapist=therapistProfiles.objects.distinct().filter(name__contains=str(self.therapist_name)[0:10])
It works well but when I change the string length to 11 or longer it returns None.
The therapist model has a one to one relationship to users model but
customer model not.
class customerprofile(models.Model):
c_name= models.CharField(max_length=40, null=True, blank=True)
class therapistProfiles(models.Model):
user=models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True)
name=models.CharField(max_length=30, null=True, blank=True)
Solution
In the visit model the costumer_name and therapist_name are instances of costumerprofile and therapistprofile. you can replace them as below:
class visit(models.Model):
costumer = models.ForeignKey(costumerprofile, on_delete=models.CASCADE)
therapist = models.ForeignKey(therapistProfiles, on_delete=models.CASCADE, default=1)
You can directly access the related fields and their attributes without needing to fetch the objects using get()
Answered By - hana-chg
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.