Django Query: lookup with variable vs. hard coded value
I'm trying to query an object and when I hard code the value it works but when I use a variable for it the query doesn't work.
Here's the class:
class AdvertisementType(models.Model):
type = models.CharField(max_length='40')
description = models.CharField(max_length='80')
def __unicode__(self):
return '%s' % self.type
Here's the query:
self.type_ad = AdvertisementType.objects.get(type=type_of_ad)
As an example, there is an AdvertisementType where the type="Inner Page"
When I use this statement:
self.type_ad = AdvertisementType.objects.get(type="Inner Page")
Everything works fine but if I do
self.type_ad = AdvertisementType.obje开发者_开发知识库cts.get(type=type_of_ad)
I get the error
Caught an exception while rendering: AdvertisementType matching query does not exist.
even when type_of_ad = "Inner Page"
Any ideas?
I figured it out. The problem is when I called
tag_name, number, type_ad = token.split_contents()
I forgot that type_ad gets the "" from the template tag.
When I chagned
self.type_ad = AdvertisementType.objects.get(type=type_of_ad)
to
self.type_ad = AdvertisementType.objects.get(type=type_of_ad[1:-1])
everything worked
精彩评论