[CentralOH] Counting References in Django: Ditch the .id

jep200404 at columbus.rr.com jep200404 at columbus.rr.com
Fri Dec 7 02:12:10 CET 2012


On Thu, 6 Dec 2012 19:43:23 -0500, jep200404 at columbus.rr.com wrote:

> In Django, what's a better way of counting references to a record 
> of an arbitrary model than my following working code? 
> 
>     c = 0
>     for r in record._meta.get_all_related_objects():
>         c += r.model.objects.filter(
>             **{r.field.name + '__exact': record.id}).count()

Ditch the .id:

    c = 0
    for r in record._meta.get_all_related_objects():
        c += r.model.objects.filter(
            **{r.field.name + '__exact': record}).count()



More information about the CentralOH mailing list