Django passing querysets between pages - django

I have a view which does a pretty heavy query on three separate databases and outputs the count of each query into a template. From the the user can go to one of the three results and get the detailed results. I don't want to repeat myself in my views and I want to maximize performance, so my question is what is the best way to achieve this result?
Can I pass a queryset to another page (get or post)?
Should I save the queryset in a session (I am not currently using session for this project as it is fully public)
Or is there a better way?
I know this is not a highly specific question, but I would appreciate any advice on this (I'm guessing - not uncommon) situation.

From my experience, I think saving your result on session is a good and simple solution.

Save the result of the querysets to the session. When I say to save the results, what I mean is to run list() on it to execute them, and then store the resulting list in request.session.

sounds like you want to have a look at caching

Related

Django Master/Detail

I am designing a master/detail solution for my app. I have searched for ever in the django docs, also here and elsewhere I could, so I guess the answer is not that obvious, despite being an answer many people look for - not only in django, but in every language, I think.
Generally, in most cases, the master already exists: for example, the Django Docs illustrate the Book example, where we already have an Author and we want to add several Books for that Author.
In my case, the parent is not yet present on the database think of a purchase order, for instance.
I have thought to divide the process in two steps: the user would start to fill in the info for the master model (regular form) and then proceed to another view to add the lines (inline formset). But I don't think this is the best process at all - there are a lot of possible flaws in it.
I also thought about creating a temporary parent object in a different table and only having a definitive master when the children are finally created. But it still doesn't look clean.
Because of that, for my app it would be ideal to create the master object at the same time as the detail objects (lines) - again, like an order.
Is there a way where I can have the same view to manage both master and detail? Like this I would receive both in the same POST request and it would make a lot more sense, not to say it would be much cleaner.
Sorry if it's too long, and thank you in advance!
So I found out that in my case the process could actually be split in two phases.
For this I simply use the traditional model form and inline formset.
But! I also found out that there could be several answers to this:
We could get crazy and build some spaceship in AJAX that would get the job done, simply by sending a JSON object (in which the lines could be an array of objects)
Django also has its ways and it's possible to send multiple forms in the same request! (thank you #mousetail for the tip).
Of course, be there as it may, there are many ways to build a house, these are just the ones I found out.

How do you pass or share variables between django views?

I'm kind of lost as to how to do this:
I have some chained select boxes, with one select box per view. Each choice should be saved so that a query is built up. At the end, the query should be run.
But how do you share state in django? I can pass from view to template, but not template to view and not view to view. Or I'm truly not sure how to do this. Please help!
Put the values to hold into the session.
There are many ways... in the view to template... put the variables in hidden fields in the forms. So when you "submit" in the subsequent forms... the values are then contained in the following request.POST.get().
Of course you can also store the various data elements in a DB table (disk or ram) between views... using the session_id as the key into the datastore. (not recommended for load balanced systems).
And my least favorite is cookies. (see the APIs for how to store in cookies)
UPDATE: Sorry there are no code examples here... the docs are pretty easy to read. There is also a djangosnippets website that you use to look up example code.
You can store such information in session as Ignacio Vazquez-Abrams said or use django-flash - (django-flash usage)

Django + Haystack how to do this search

I'm new to Haystack and to the search world so I didn't know how to ask this question.
What I want to achieve is the following.
Having a search query like: one two
I would like to get returned any content like:
This one
one
two
two one
something one here
Is this possible with Haystack + solr/xapian?
Is also possible to have a relevance on the result? In the case where both words are hit, that would give more relevance to me.
I'm currently using SearchQuerySet in my view but can't achieve that.
Cheers
So you're basically looking for an OR type query right? By default haystack uses an AND operation for joining queries.
You can do this two ways:
Change HAYSTACK_DEFAULT_OPERATOR within your settings.py to be OR. This will obviously be a site-wide change.
Modify your SearchQuerySet form to use filter_or which will force the OR style lookup. So pass a new one into your form/view: SearchQuerySet.filter_or(**kwargs)
Apart from that, you could always join Django Q objects together but considering you have these options, those are probably your best bet.
For relevance, you should read the Best Practices page which goes into using search templates and making them be your way to show relevant content.
Hope that helps!

django - the best way to combine pagination with filtering and request.POST - like stackoverflow - ajax?

I want to combine pagination with filtering. Since I have a lot of filters I do not want to send them per GET request, since the URLs get really ugly.
Since django pagination uses GET request to pass the page parameters, I do not know how I can combine these two approaches.
Any idea?
Great add-on would be: How can I combine this approach with table sort? :-)
Edit:
Actually it should work like the pagination of stackoverflow - user questions. If a user clicks on a page number one is shown the correct page, without showing the get parameters in the url.
This is the url called.
https://stackoverflow.com/api/userquestions.html?page=2&pagesize=10&userId=237690&sort=Recent
But the url shown in the browser is neat and short.
Seems to be ajax. Anybody an idea how to implement this? :)
If the URL is not shown in the browser`s address bar, I do not care about whether it is beautiful or not.
Edit: The solution:
Make an ajax update with all filter parameters passed to the view. This should help you get started with implementing ajax for your site: link
Thus the GET parameters never show up in the address bar.
have you checked the paginate application for django?
it may help you a lot, use it all the time :D
http://code.google.com/p/django-pagination/
Have you considered django-tables2? It gives you django-admin style tables without you having to write the logic yourself.
maybe you can use the urs, something like:
http://oursite.com/something/filter1/filter2/3/
the doc -> http://docs.djangoproject.com/en/1.1/topics/http/urls/
I figured out two solutions:
Instead of using just hyperlinks use it inside a POST form, i dont have any example now but i remember have used that for REST functions in Ruby on rails
Save the query info in a session.
Hope this help.

about annotate django

I'd like to create the top five best seller of the products in each month.
I've heard that the annotations must be used for this case but I don't know how to use it.
Will anybody be kind enough to help me ??
You need to provide much more information before anyone can provide you with a useful answer. Describe what you know, what you've tried, elaborate on what you hope to accomplish.
In the mean time, you can learn about django's annotate() function, and see a few examples by reading the aggregation documentation for django.
annotate is not the only way to do it. You could also use an extra aggregation, though it might not be very efficient, and this will rely on sql specific to your database. This approach will not work in App Engine, but should in a SQL compliant database.
For example, say you had a Product model and a SalesOrder model, where the SalesOrder model has a line item for each product_id and order_date in it.
top_products = Product.objects.extra(
select={'num_orders':'select count(product_id) from app_product where app_salesorder.order_date > thirty_days_ago and app_product.id = app_salesorder.product_id'})
.order_by('-num_orders')[:5]