Note that there are some explanatory texts on larger screens.

plurals
  1. POdjango dictionaries for model method results
    primarykey
    data
    text
    <p>For these models:</p> <pre><code>class Publisher(models.Model): name = models.CharField(max_length=30) code = models.SmallIntegerField(unique=True) class Book(models.Model): date = models.DateField(auto_now_add=True) publisher = models.ForeignKey(Publisher) hardback = models.BooleanField() large_print = models.BooleanField() foreign_lang = models.BooleanField() </code></pre> <p>For each boolean field, I want to have the Publisher sorted by percentage of that field, and total book count.</p> <p>Eg <h3>Large Print </h3></p> <p>Publisher - Percentage Large Print - Total Books</p> <p>I've got model methods Publisher.get_percentage and Publisher.total_count that calculates these when supplied with a date range (d1, d2) and the relevant boolean field (choice).</p> <p>d1 and d2 will be selected by the user from a form.</p> <p>I'm working on a view like this:</p> <pre><code>def data(request): if request.method == 'POST': form = DateForm(request.POST) if form.is_valid(): cd = form.cleaned_data choices = ['publisher__hardback','publisher__large_print','publisher__foreign_lang'] for choice in choices: ch = {} pub = Publisher.objects.all() for p in pub: ch = {p.code : p.total_count : p.get_percentage(cd.d1, cd.d2, choice)} </code></pre> <p>What's the best way to create a new dictionary for each choice, and append the values to the dictionary ?</p> <p>I think I need to create these dictionaries in the view (rather than just iterating through the publishers in a template) as I want to order the results by percentage...</p> <p>Is there a better way to achieve all of this ?!</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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