Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could modify the <code>get_query_set()</code> method to return a custom QuerySet, adding the methods you require. In your case, you would use:</p> <pre><code>class TransactionManager(models.Manager): def get_query_set(self): return TransactionQuerySet(self.model) class TransactionQuerySet(models.query.QuerySet): def reasonably_complex_filter(self): return self.filter(...) </code></pre> <p>I've seen examples subclassing the TransactionQuerySet into the <code>Transaction</code> model, or in the related <code>Manager</code>, but that is entirely up to you.</p> <p><strong>edit</strong>: I seem to have overlooked the fact that <code>objects</code> first references to the <code>TransactionManager</code> and therefore <code>Transaction.objects.reasonably_complex_filter()</code> isn't possible in my implementation. This can be fixed in three ways:</p> <ul> <li>Implement the <code>reasonably_complex_filter</code> in both the Manager and the QuerySet;</li> <li>Use <code>Transaction.objects.all().reasonably_complex_filter()</code> when that's the only filter required;</li> <li>Refer to Marcus Whybrow's answer for a solution that will implement the method in both the <code>QuerySet</code> and the <code>Manager</code> without code duplication.</li> </ul> <p>It depends on the application which option is the most desireable, although I would strongly recommend against code duplication (although you could use a global method to overcome this). Though, the last option could be too costly in terms of overhead if you only require this kind of practice once, or if you only intend to use the complex filter in combination with another filter.</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