python - Return exact matches at top of Django Queryset -


I have a DJongo model, which is called "user", which keeps some basic information about people, i.e. First and last name Currently there is a simple search in my Digenga model, where a user types in first name, then returns the Digengo query in the first 10 games, which is called the Last command are given. For example, currently, if you search for "Sam" you can get the following results:

  1. Sam Abbott
  2. Samuel Becker < Sam Seymour
  3. Sam Simmons

    Its code is simple:

      User.objects.filter (Q (However, I I would like to change it so that any  precise  will be returned before the first name match, after which the remaining result if someone type in "Sam", the result will be instead:  
    1. Sam Abbott
    2. Sammy Rogers

      (Last name matches first, sorted by last name , After all the fairs according to the previous name).

      I thought it was changed to 2 queries and then just in combination with lists, but I was thinking that it is possible to do this in 1 query , Originally sticking to the original DJGonga Carriet API (instead of typing a closed query) Does anyone know one way to do this?

      Thanks in advance.

      I do not think that it is actually possible that with only one query (at least Django ORM)

      Then your 2 questions should look like this:

        limit = 10 q1 = User.objects.filter (first__iexact = token) .order_by ('last') [: Limit] limit - = len (q1) if limit: q2 = user.objects.exclude (pk__in = q1) .filter (first__istartswith = token). Order_by ('last') [: limit] else: q2 = [] users = list (q1) + list (q2)   

      Another way to do this is to query the filter dragon , But you have to get all the results, not only the last 10:

        query = user.objects.filter (first__istartswith = token) .order_by ('last') = exact user in the query If user for first == token] other = [User for query in user, if user. First! = Token] User = Exact + Other    

Comments