I think that might be a better way, but the data should never change so I'm
ok with the denormalization. How would I access the po_num in that example?

Order.edi.po_num? or Order.PoNum.po_num?

I think though I was asking the wrong question. I updated this on Stack
Overflow.

What I really need to do is join the two tables so I can do a WHERE on a
> count of the related table. I would like to filter for all Order objects
> that have 0 related EDI856 objects.


At first I was thinking of putting a 'count' field in each model and having
the update logic in the overridden save() functions. This would have the
benefit of spending the computing costs up front instead of on every search
(there are a few hundred thousand records in each table). I imagine this is
a pretty common task but from Googling it seems there's no core django way
to do it, you just have to do it yourself. Is that correct?

I did find however another way to query for the counts using annotations.
http://docs.djangoproject.com/en/dev/topics/db/aggregation/#generating-aggregates-for-each-item-in-a-queryset

Annotations provide exactly the functionality I'm looking for so I'm going
to try them first, but something tells me adding the count field is the
right way to go.

-Francis

---
Francis Gulotta
wiz...@roborooter.com


2010/4/13 Ian Lewis <ianmle...@gmail.com>

> wizard,
>
> Would it make sense to use the intermediate table to hold the field?
>
> Using the stack overflow example:
>
> class Edi856(models.Model):
>     pass
>
> class Order(models.Model):
>     edi = models.ManyToManyField(Edi856, through='PoNum')
>     def in_edi(self):
>         '''Has the edi been processed?'''
>         return self.edi.all().count()
>
> class PoNum(models.Model):
>     order = models.ForeignKey(Order)
>     edi = models.ForeignKey(Edi856)
>     po_num = models.CharField(max_length=90, db_index=True )
>
>
> The save route also sounds ok, a pre_save signal could also work.
>
>
> On Wed, Apr 14, 2010 at 5:19 AM, wizard <wiz...@roborooter.com> wrote:
>
>> This is sort of an extension of my post from stackoverflow.
>>
>> http://stackoverflow.com/questions/2632573/how-do-i-relate-two-models-tables-in-django-based-on-non-primary-non-unique-keys
>>
>> The gist is I have two tables with a common field and I would like to
>> relate two models with a many to many based off of this field.
>>
>> As far as I can tell I have to build the relations manually. I was
>> thinking of overriding the save method in both models to query the
>> other table and rebuild the relation on save.
>>
>> Does this sound rational?
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To post to this group, send email to django-us...@googlegroups.com.
>> To unsubscribe from this group, send email to
>> django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com>
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/django-users?hl=en.
>>
>>
>
>
> --
> =======================================
> 株式会社ビープラウド  イアン・ルイス
> 〒150-0021
> 東京都渋谷区恵比寿西2-3-2 NSビル6階
> email: ianmle...@beproud.jp
> TEL:03-6416-9836
> FAX:03-6416-9837
> http://www.beproud.jp/
> =======================================
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com<django-users%2bunsubscr...@googlegroups.com>
> .
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to