Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsupported lookup 'istartwith' for CharField or join on the field not permitted

Tags:

python

django

I created some models using Django ORM.

class feed(models.Model):
    location = models.OneToOneField('feedlocation')

class feedlocation(models.Model):
    areaHash = models.CharField(max_length=100,default='')

Then I used the follow code to find out the 'feed' on the same areaHash.

Feed.objects.filter(location__areaHash__istartwith='*****')

The I got this error:

FieldError: Unsupported lookup 'istartwith' for CharField or join on the field not permitted.

What should I do to achieve this query?

like image 617
living zhang Avatar asked Jun 25 '15 05:06

living zhang


2 Answers

This code is incorrect :

Feed.objects.filter(location__areaHash__istartwith='*****')

Try :

Feed.objects.filter(location__areaHash__istartswith='*****')
like image 151
coder3521 Avatar answered Nov 01 '22 21:11

coder3521


Another Workaround could be using icontains (keeping case-insensitivity as @shacker noticed) :

Feed.objects.filter(location__areaHash__icontains='*****')
like image 26
HamzDiou Avatar answered Nov 01 '22 22:11

HamzDiou