Database queries location

61 views
Skip to first unread message

pythonista

unread,
Dec 18, 2014, 4:12:05 PM12/18/14
to django...@googlegroups.com
I understand that functions can be placed in the models fille

However if I have complex queries that receive post input does the


Query live in the model the views or an external module

Lachlan Musicman

unread,
Dec 18, 2014, 4:48:26 PM12/18/14
to django...@googlegroups.com
I usually put those functions in the view
cheers
L.
> --
> 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/45f5d942-2ede-49bb-8ec1-d5a2486bd656%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
------
"We are at once both obnoxious and indispensable." - John Ngumi on the Kikuyu
in It's Our Turn To Eat, by Michela Wrong.

aRkadeFR

unread,
Dec 19, 2014, 7:06:54 AM12/19/14
to django...@googlegroups.com
Depend exactly on what to compute for your post data.

I would almost write no logic code in the views.

I split every application as follow:
- managers.py: All the logic as a table level (raw SQL, complex queries)
- models.py: All logic as a row level / object level (python computing data)
- forms.py: All logic as forms data / processing / validation for
request data

Have a good one,

aRkadeFR

pythonista

unread,
Dec 20, 2014, 9:28:57 AM12/20/14
to django...@googlegroups.com, con...@arkade.info
Thank you for your response.  

Most of my data is complex raw sql.
I had placed it in the views and it is working as expected

I would like to migrate the raw sql to managers, 
Can you point me to one or more good examples of how the manager is coded and how the sql interacts with the views.

Thanks

Collin Anderson

unread,
Dec 22, 2014, 8:52:04 PM12/22/14
to django...@googlegroups.com, con...@arkade.info
Hi,

There's a good example of creating a custom manager here:

Collin

pythonista

unread,
Dec 23, 2014, 1:49:49 PM12/23/14
to django...@googlegroups.com, con...@arkade.info
Can this be used with a complex query in which multiple tables (classes) are being joined? 

I also have to pass multiple post values to the query.

Thanks for the example

Collin Anderson

unread,
Dec 26, 2014, 4:41:12 PM12/26/14
to django...@googlegroups.com
Hi,

That should all be possible.

from django.db import models

class MyManager(models.Manager):
   
def complex_query_with_multiple_tables(self, mutiple, post, values):
       
# put your query here.

Collin
Reply all
Reply to author
Forward
0 new messages