sql - Django: Multiple COUNTs from two models away -


I am trying to create a profile page that shows the amount of dwarf that is assigned to each related career. I have 4 careers, each of them has 2 jobs and of course there are many dwarves, each of them has a single job. How can I get the number of dwarfs in each of those careers? My solution was to highlight career names in HTML and to ask a question for every career but it seems like a lot of questions.

Here's what I want to:

  Assigned: 3 Build: 2 Farming: 1 House: 1  

These are my models. I add some complications (they are related to their jobs) by not adding careers directly to their midget model. Import django.contrib.auth.models from

  import django.db import model class carrier (models.Model) from user: name = models.CharField (MAX_LENGTH = 64) def __unicode __ (manually ): Return self.name class job (models.Model): career = models.ForeignKey (carrier) name = models.CharField (MAX_LENGTH = 64) career_increment = models.DecimalField (max_digits = 4, decimal_places = 2) job_increment = models DecimalField (max_digits = 4, decimal_places = 2) def __unicode __ (self): return self.name class dwarf (models.model): work = models.ForeignKey (job) user = models.ForeignKey (user) created = models.DateTimeField (Auto_now_add = true) Modified = models.DateTimeF Ield (auto_now = true) name = models.CharField (MAX_LENGTH = 64) square meta: verbose_name_plural = 'dwarf' def __unicode __ (self): Return to Lf.name  

Edit 1 My thoughts look something like this:

  Diff Fort (Request): Career = Career.  

More templates:

 () '' ''  for careers in careers%}} 
  • gt; {{Career.dwarf_set__count}} & lt; / Li & gt; {% Endfor%}
  • is error:

    The keyword 'dwarf_set' can not be resolved in the field. The options are: id, job, name

    resolution

    view:

      DIF castle (request )): Career = Career.objects.all (.) Interpretation (dwarfs_in_career = Calculation ('job__dwarf')) Return render_to_response ('ragna_base / fortress.html', {'Career': Career})  

    Template:

    {% Return to Career Career%}
  • gt; {{Career.name}}: {{career.dwarves_in_career}} & lt; / Li & gt; {% Endfor%}

    Even better solution

      careers = Career.objects.filter (Q (job__dwarf__user = 1) Import cue (job__dwarf__user__isnull = true)) .notate (dwarves_in_career = calculation ('job__dwarf'))  

    Do not forget to django.db.models for count, q |

    What I like about the solution given above is not only the career of the darling, but even in those careers, Not that, which was the next problem I encountered. Here's my idea of ​​perfection:

      & lt; Ul & gt; {% For Career Career}} 
  • gt; {{Career.name}}: {{career.dwarves_in_career}} & lt; / Li & gt; {% Endfor%} & lt; / Ul & gt;
  • What is it for you to do?

    Import from Django.db.models Career.objects.anotate (count ('dwarf'))

    Now each carrier object must have a dwarf__count property.


    Comments

    Popular posts from this blog

    oracle - The fastest way to check if some records in a database table? -

    php - multilevel menu with multilevel array -

    jQuery UI: Datepicker month format -