Awesome, it works, thanks!

On Saturday, June 22, 2013 4:52:34 PM UTC+2, Goran wrote:
>
> Just add related name to the artist field in the Drawinds model artist = 
> models.ForeignKey(Artist, related_name='drawings')
>
> Then you can use X.drawings.count() when is needed. It also works in the 
> template, Let say that you have list with some artists named 'foo' so in 
> the template you can count his drawings on the following way:
>
> {% for x in foo %}
>
>    {{ x }} -  {{ x.drawings.count }} 
>
> {% endfor %}
>
> It will be displayed as:
> Donatello - 24
> Michelangelo - 14
>
> On Saturday, June 22, 2013 4:36:54 PM UTC+2, thomaaaas wrote:
>>
>> Hello,
>>
>> In my model I have artists and drawings. Each drawing has 
>> a ForeignKey(Artist)
>> I'm trying to count the number of drawings each artist has, but I don't 
>> know how to do it...
>> Below a simplified version of my code.
>>
>> class Artist(models.Model):
>> name = models.CharField(max_length=140)
>>
>> def number_of_drawing(self):
>> return drawing.count() # how to do this ?
>>
>> class Drawing(models.Model):
>> artist = models.ForeignKey(Artist)
>> description = models.TextField()
>>
>> Can you help me write the number_of_drawing function?
>> Thanks!
>>
>>

-- 
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.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to