{% for user in object_list %}
         <tr>
             <td>{{ user.id }}</td>
             <td>
                 Number of assets: {{ user.assets_set.count }}
             </td>
        </tr>
    {% endfor %}


[]'s

Lucas Magnum.


2014-02-03 hykyd <chrisvele...@gmail.com>:

> My problem is simple: I have **Users** who own **Assets** or **Assets**
> which belong to Users If you prefer and I cannot make it to retrieve the
> number (count) of **Assets** each **User** has. I know this might be sound
> silly to most of you but I am new to python/django (coming from PHP/MySQL)
> and I do not know how things work here. I do not want to be engaged with
> raw SQL - this would be my last choice If nothing else works.
>
> (*) I have removed all non-related raws from the code
>
> **Users**
>
>     class Users(models.Model):
>         firstname = models.CharField(max_length=100)
>         lastname = models.CharField(max_length=100)
>
> **Assets**
>
>     class Assets(models.Model):
>         serial = models.CharField(unique=True, max_length=100)
>         user = models.ForeignKey('Users', blank=True, null=True)
>
>         # this is what I am playing with to retrieve the number of assets
> each user owns
>         @classmethod
>             def user_assets(self):
>                 return Assets.objects.filter(user=user).count()
>
> **views.py**
>
>     class UserList(ListView):
>         model = Users
>         def get_context_data(self, **kwargs):
>             context = super(UserList, self).get_context_data(**kwargs)
>             context['user_assets'] = self.model.user_assets()
>             return context
>
> **template**
>
>     {% for user in object_list %}
>          <tr>
>              <td>{{ user.id }}</td>
>              <td>
>                  {{ user_assets }}
>              </td>
>          </tr>
>     {% endfor %}
>
> How can I get that number? I have read about aggregations, annotations and
> filters but can't really get it.
>
> I am looking for a simple solution by using class based views and easily
> expandable (I may want to add other models later)
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/e43421ba-5d8f-4f89-85ca-edeb6ff6e1f3%40googlegroups.com
> .
> For more options, visit https://groups.google.com/groups/opt_out.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAAB7jOwJNjPC6LkPcK26eMZN463%2BZHXi65Xg%2B1ziwQjVfX9x8Q%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to