Friday, October 11, 2013

Re: Django query from database

Ok, I think I got the point.

So, correct me if I didn't get it right, you have to get users which have first_name, last_name, or other fields equal to the words in your list B.

This can be accomplished by looking at each field for each keyword. I would also compile a dictionary keeping reference of what field matches the search
Continuing the code I wrote above:

users = {}
for field in ['first_name', 'last_name']:
    # build something like {'first_name__in', ['David', 'Henry', 'Ostro', ' Henroo']}
    filter_dict = {field + '__in': B}

    # find all users that match
    results = User.objects.filter(**filter_dict)

    # store each user and the list of its fields that match the search
    for user in results:
        try:
           users[user].append(field)
        except KeyError:
            users[user] = [field]

This is perhaps the worst Python code I ever wrote, but I hope you can tolerate it. Also beware that it is written directly in the mail composer (so indentation is wrong and code is untested).

Now I realize that the "__in" trick can be used also for the first code I wrote, try it.

As for the template, you can pass the users dict in the context. In the template loop over the key/values with

{% for user,fields in users.iteritems %}
{% endfor %}

inside this loop you can further loop over fields with

{% for field in fields %}
{% endfor %}

I am not sure if inside this last loop you can write something like

{{ user.field }}

since I have to check if the template attribute retrieving syntax also performs getattr. if this does not work you have to store tuples as values of the users dict, instead of fields, each tuple being (field, getattr(user, field)).

Try to give a sense to all this stuff and let me know!

Cheers,
Leo



Leonardo Giordani
Author of The Digital Cat
My profile on About.me - My GitHub page - My Coderwall profile


2013/10/11 Kamal Kaur <kamal.kaur188@gmail.com>
On Fri, Oct 11, 2013 at 6:43 PM, Leonardo Giordani
<giordani.leonardo@gmail.com> wrote:
> Sorry, I forgot the User part of your question.
>
> Let me understand the exact relationship between codes, words and users: do
> those words or codes come from a form? Or are saved in the DB for each user?

Actually I have to search for client details (from UserProfile table)
ignoring vowels. Because new users come from time to time. During
their registration, the phonetic codes of all the words entered are
saved (in CodeTable) along with the actual word.

When a string is searched for, in search box, it is split into words
and then these words are converted to phonetic codes which are to be
searched in CodeTable. The words corresponding to matching codes will
be searched for in UserProfile table which will be given as output for
searched keywords i.e No matter if a user enters "Smyth" or "Smith",
output will be on the basis of their sound. And thats all :)

<snip>

> If you find the time to better describe the exact flow of your application I
> hope I can help you find a good solution for your problem.

Here you go :)

--
Kamaljeet Kaur

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAP8Q%2Bxi-%2BPibMh_DsMyf14UUpZ_BkSEw5nWTRkdYyzoCDkpJWA%40mail.gmail.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAEhE%2BO%3DA%2B1MrXVBEYVS6fhvXGG4Yiem5t6wKdjzrfLnRG62QNw%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate