Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h3>first idea:</h3> <p>in the view you could query for today's <code>ProductStatistic</code>, than loop over the the queryset and add a variable <code>ranking</code> to every object and add that object to a list. Then just sort after <code>ranking</code> and pass the list to ur template.</p> <h3>second idea:</h3> <p>create a filed <code>ranking</code> (hidden for admin) and write the solution of ur formula each time the object is saved to the database by using a <a href="http://docs.djangoproject.com/en/dev/topics/signals/#topics-signals" rel="nofollow noreferrer">pre_save-signal</a>. Now you can do <code>ProductStatistic.objects.filter(date=today()).order_by('ranking')</code></p> <p>Both ideas have pros&amp;cons, but I like second idea more</p> <h3>edit as response to the comment</h3> <ul> <li><p>Use Idea 2</p></li> <li><p>Write a view, where you filter like this: <code>ProductStatistic.objects.filter(product= aProductObject, date__gte=startdate, date__lte=enddate)</code></p> <p>loop over the queryset and do somthing like <code>aProductObject.ranking+= qs_obj.ranking</code></p></li> <li><p>pass a sorted list of the queryset to the template </p></li> </ul> <p>Basically a combination of both ideas</p> <h3>edit to your own <a href="https://stackoverflow.com/questions/1895263/sorting-products-after-dateinterval-and-weight/1907304#1907304">answer</a></h3> <p>Your solution isn't far away from what I suggested — but in sql-space.</p> <p>But another solution:</p> <p>Make a Hit-Model:</p> <pre><code>class Hit(models.Model): date = models.DateTimeFiles(auto_now=True) product = models.ForeignKey(Product) purchased= models.BooleanField(default=False) session = models.CharField(max_length=40) </code></pre> <p>in your view for displaying a product you check, if there is a Hit-object with the session, and object. if not, you save it</p> <pre><code>Hit(product=product, date=datetime.datetime.now(), session=request.session.session_key).save() </code></pre> <p>in your purchase view you get the Hit-object and set <code>purchased=True</code></p> <p>Now in your templates/DB-Tools you can do real statistics.</p> <p>Of course it can generate a lot of DB-Objects over the time, so you should think about a good deletion-strategy (like sum the data after 3 month into another model <code>MonthlyHitArchive</code>)</p> <p>If you think, that displaying this statistics would generate to much DB-Traffic, you should consider using some caching.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload