Need to search through several tables using one model

58 views
Skip to first unread message

Damjan Dimitrioski

unread,
Dec 12, 2014, 10:09:30 AM12/12/14
to django...@googlegroups.com
Hi,

Is there a way to make a model that links several db tables in one model, so that can be used in a change list view ?
Most importantly, I need to be able to use the search field, can this be accomplished ?

Regards.

Vijay Khemlani

unread,
Dec 12, 2014, 11:41:08 AM12/12/14
to django...@googlegroups.com
Just using the ORM I think not

You could

1. Make a raw sql query using UNIONs for each table

2. Make a common superclass for the models with th search field

3. Use an external search engine (elasticsearch, etc) and store the entries for the required models under the same index.

Finally, you could just query each model separately

--
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...@googlegroups.com.
To post to this group, send email to django...@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/0fc05894-fb00-40d5-b1d9-68daa5c82841%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Damjan Dimitrioski

unread,
Dec 13, 2014, 10:55:33 AM12/13/14
to django...@googlegroups.com
Can I override the search button, so it will call the custom raw query ?
And if yes, can you show me how to override it, please ?

Alon Nisser

unread,
Dec 13, 2014, 12:23:56 PM12/13/14
to django...@googlegroups.com
You could also create a model that foreign keys to all the relevant models, and filter on that.. 
Buy first you need to clarify (for your self..) The exact use case: is it search? if so better use haystack with some search backend (elasticsearch would be a great choice), Is it to choose something in a form? something else? 

The technical details should be derived from the problem you are trying to solve.. 

Vijay Khemlani

unread,
Dec 13, 2014, 5:06:46 PM12/13/14
to django...@googlegroups.com
What search button are you talking about?

--
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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Damjan Dimitrioski

unread,
Dec 13, 2014, 5:27:23 PM12/13/14
to django...@googlegroups.com
The search filter above the change list view template, there is a button named search I think, I think it's need to overriden in order to implement custom model or something.

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/oaInfa9o4Js/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Damjan Dimitrioski

unread,
Dec 15, 2014, 7:49:50 AM12/15/14
to django...@googlegroups.com
I wrapped something like this:
https://dpaste.de/4WTq

However, I'm not able to see any results in the changelist view, but the get_query_set method prints the query results in the STDOUT, what I'm doing wrong ?

Vijay Khemlani

unread,
Dec 15, 2014, 8:13:46 AM12/15/14
to django...@googlegroups.com

Damjan Dimitrioski

unread,
Dec 15, 2014, 8:45:10 AM12/15/14
to django...@googlegroups.com
Oh, sorry about that, it was se to an hour, here is an updated one: https://dpaste.de/q9sq

Collin Anderson

unread,
Dec 16, 2014, 8:03:20 AM12/16/14
to django...@googlegroups.com
Hi,

The new link isn't working either.

Collin
Reply all
Reply to author
Forward
0 new messages