Thursday, January 26, 2017

result of filter query returning empty because of def __unicode__(self)

Does adding a def __unicode__(self): method into a model affect the queries being returned ?


I am currently experiencing a situation in which the presence of def __unicode__(self): in the model is interfering with the results being returned. 


These two are my models

   class modelLabTestName(models.Model):          test_category = models.ForeignKey(modelLabCategory)          test_name = models.CharField(max_length=128, unique=True , default="None")          def __unicode__(self):                return self.test_name   #---------------------->--- Line A----                #return "Test Name : " + self.test_name ------->--- Line B----    class modelNormalLabTest(models.Model):      test_name = models.ForeignKey(modelLabTestName,default=None)      field_name = models.CharField(max_length=128)      field_value = models.CharField(max_length=128)


Now notice in the above there are two lines A and B. Currently Line B is commented and this query works and I get back an object

lab_entries= modelNormalLabTest.objects.filter(test_name__test_name="ABC")


However if I replace

return self.test_name 

in the model modelLabTestName with this instead (Line B)

return "Test Name : " + self.test_name

The query fails (i.e - No object is returned)


My question is why is this happening and if there is any way for me to fix this issue. I already checked the value of the test_name property with the following statement and it is returning the correct value

t = modelLabTestName.objects.all()[0].test_name #t = "ABC"

then why is

lab_entries= modelNormalLabTest.objects.filter(test_name__test_name="ABC")

or even this

 modelLabTestName.objects.get(test_name="ABC");

returning nothing ? when the model modelLabTestname has the following method in it

def __unicode__(self):          return "Test Name : " + self.test_name 

Any suggestions would be appreciated. I am using Django (1.10.5)


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bb0e5164-751e-4a85-b71f-72e4c69d49d1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment