lundi 18 avril 2016

Django Query optimization with 7 requirement

I'm creating a website using Django. I have some questions about query: Suppose I have a model:

class M (models.Model):
    likes = models.IntegerField()
    name = models.Charfield()

class A (models.Model):
    choice1 = models.BooleanField(blank=True) 
    choice2 = models.BooleanField(blank=True)
    choice3 = models.BooleanField(blank=True)
    choice4 = models.BooleanField(blank=True)
    choice5 = models.BooleanField(blank=True)
    choice6 = models.BooleanField(blank=True)
    choice7 = models.BooleanField(blank=True)
    key = models.ForeignKey(M, null = true)

class AForm(forms.Form):
    def __init__(self, *args, **kwargs):
        kwargs.setdefault('label_suffix', '')
        super(AForm, self).__init__(*args, **kwargs)
    choice1 = forms.BooleanField(initial=False) 
    choice2 = forms.BooleanField(initial=False)
    choice3 = forms.BooleanField(initial=False)
    choice4 = forms.BooleanField(initial=False)
    choice5 = forms.BooleanField(initial=False)
    choice6 = forms.BooleanField(initial=False)
    choice7 = forms.BooleanField(initial=False)

I will create these choices as checkbox and make user enter a string(name) in a search bar. It will then query for a list of M based on choices and name.

The ideal query would be a perfect match that matches all the choices and a name. I know I should use name__icontains for best query results. This is to be the first items in the final query list. Then it would check for 6 matches in 7 matches and then 5 out of 7 and so on. All of them should be ordered by likes. And the final query list should be a size of 20.

Could some one tell me what the best or neat way to write this query in a efficient way for Django. Thank you.




Aucun commentaire:

Enregistrer un commentaire