Newtt
Newtt

Reputation: 6190

Django invalid literal for int() with base 10

I'm working a simple search result page in Django.

The user searches by text and adds a city field from a dropdown.

The query is as follows:

if 'query' in request.GET and request.GET['query']:
    city = request.GET['city']
    q = request.GET['query']
    ct = Classified.objects.filter(name__icontains=q).filter(city__exact=city)
    return render(request, 'template/search.html', {'ct': ct, 'query': q})

The model for Classified is as follows:

class Classified(models.Model):
    name = models.CharField(max_length=256, unique=True)
    email = models.CharField(max_length=100)
    category = models.ForeignKey(Categories)
    phone_number = models.IntegerField(max_length=10, default=0)
    address = models.CharField(max_length=1000)
    id = models.IntegerField(primary_key=True)
    city = models.ForeignKey(Cities)
    image = models.ImageField(blank=True, upload_to='static/img/')

    def __unicode__(self):
        return self.name

Cities is as follows:

class Cities(models.Model):
    name = models.CharField(max_length=256)

    def __unicode__(self):
        return self.name

On search, the /search/ page gives me the following error:

invalid literal for int() with base 10: 'Searched-city'

What am I missing out on?

Upvotes: 2

Views: 8182

Answers (1)

Bryan
Bryan

Reputation: 6752

City is a foreign key, you need to update your query to proxy through the relationship by explicitly saying city__name__exact

ct = Classified.objects.filter(name__icontains=q).filter(city__name__exact=city)

Upvotes: 5

Related Questions