Friday, October 11, 2013

Re: Django query from database

I think that you have to perform a query for each Code, extracting the list of Word matching that Code and then merging all the lists.
Since I don't know your models, I'll give you some general code, let me know if you understand how to implement it exactly on your models.

Assuming that the CodeTable is the representation of a Word model:

B = []
for code in A:
    words = Word.objects.filter(code=code)
    B.extend(words)

This is the basic algorithm. You can then make all sorts of Python magic tricks to shorten it or make it faster, but if you are not dealing with billions of data I do suggest you to keep it simple and readable.

If you need to have each element in B appear just one time (uniqueness) just perform a B = set(B) at the end.

Let me know if it works. =)

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>
Hello there,

Hope you are doing well :)

I have a problem regarding querying a list from two tables, the
procedure goes like:

Considering two tables from mysql database:

1. UserProfile table, with complete client details: First name, Last
name, Address, email id, Contact number etc.
2. CodeTable, with only two columns 'Word' and 'Code'. Codes are
soundex codes for the words in first column like:

Word        Code
David        D130
Henry       H560
Ostro        O236
Milton       M435
Henroo    H560

These 'words' are also contained in UserProfile table somewhere, can
be in any column. I have a list A with soundex codes, entered by user,
obtained with request.GET like:
A= ['D130', 'H560', 'O236']

Here starts the problem, I have to fetch 'Words' from CodeTable
corresponding to the 'Codes' in list A, meaning that I need another
list B, like:

B = ['David', 'Henry', 'Ostro', ' Henroo']

Because the code H560 is same for ' Henroo', it will also be there in
list B as we are searching for all the words that have 'codes' in list
A.

For this list B, I want to search all the client details i.e. I want
to display all the clients that have any of the words in list B from
UserProfile table.

I am not getting how to accomplish this searching using Django
queries. I am not able to make a view/function for this logic. Please
help and feel free to ask details if problm is not clear.

Thanks ^_^


--
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%2BxhO%3DeZJjdui4OOw1T3V2QqtJW_PnC-BTTCDdBogipc8sw%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

--
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%3DFRyds-AOGAOd47mRgtjEz5u4TAKVx6S%2BPNQ5Qfb3WDg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment