How do I relate two models/tables in Django based on non primary non unique keys?
Posted
by wizard
on Stack Overflow
See other posts from Stack Overflow
or by wizard
Published on 2010-04-13T19:28:59Z
Indexed on
2010/04/13
19:33 UTC
Read the original article
Hit count: 268
I've got two tables that I need to relate on a single field po_num. The data is imported from another source so while I have a little bit of control over what the tables look like but I can't change them too much. What I want to do is relate these two models so I can look up one from the other based on the po_num fields.
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 do filter for all Order objects that have 0 related EDI856 objects.
I tried adding a foreign key to the Order model and specified the db_column and to_fields as po_num but django didn't like that the fact that Edi856.po_num wasn't unique.
Here are the important fields of my current models that let me display but not filter for the data that I want.
class Edi856(models.Model):
po_num = models.CharField(max_length=90, db_index=True )
class Order(models.Model):
po_num = models.CharField(max_length=90, db_index=True)
def in_edi(self):
'''Has the edi been processed?'''
return Edi856.objects.filter(po_num = self.po_num).count()
Thanks for taking the time to read about my problem. I'm not sure what to do from here.
© Stack Overflow or respective owner