Search code examples
mysqldjangosubstringdjango-filters

Django filter and MySQL (SUBSTRING, LENGTH)


please tell me, how i can make query into database (mysql), with Django models filters. I want create select query like:

    SELECT word FROM core_dictionary WHERE LENGTH(word)=8 AND SUBSTRING(word,1,1)='А';

Sorry for my English.


Solution

  • I found a way to solve this problem only by using the "extra":

        d = Dictionary.objects.values_list('word').extra( where=['LENGTH(word)=%s'%(int(wordlen)*2), 'SUBSTRING(word,1,1)="%s"' % startchar] )   
    

    But i think is not good way....