Re: Doing a relationship lookup from within a model.
On Tue, Oct 18, 2011 at 8:22 PM, Jack Morgan wrote: > I've got 2 tables that are related to each other. Orders and History. > Inside the History table is the 'status' column. Like so.. > > class Orders(models.Model): > 'order info' > > class History(models.Model): > timestamp = models.DateTimeField(auto_add_now = True) > order = models.ForeignKey(Orders) > user = models.ForeignKey(User) > comment = models.TextField() > status = models.CharField(max_length = 20) > > > I'm storing the status in the History table right now because I need to > keep track of who is moving the orders along. However, I need to do a > lookup on orders based on their current(most recent) status. For > simplicities purpose it seems a good idea would be to just put a status > field in the Orders table and update as the Order advances through the > stages. However, that creates duplicate data, which my client has expressed > an extreme hatred for. > > What It's like to be able to do is something like: > o = Orders.filter(status = 'new') > > I'm not entirely sure how or when relationship managers are brought in, so > I was thinking something like this in the Orders model: > status = self.history_set.values('status').order_by('-id')[0]['status'] > > But that wouldn't let me do a lookup by state. > > What's a good Django way to handle this issue? > Having the status field in the Order's column is the elegant way to do it. History model, should be like a revision-holder; mainly used for auditing. -V http://blizzardzblogs.blogspot.com/ -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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.
Re: Doing a relationship lookup from within a model.
Don't you have an issue in that History is many to on on Orders, so an order has multiple states? Unless you're expunging History (in which case, isn't it mis-named?) or changing state in all of an Order's History instances (in which case don't you lose you audit trail), all Order's will have a History instance with status 'new'. While I think you could filter Orders using history_set__status='new' and maybe other things (such as filtering on History with select_related) I'm not sure that's really what you want. If it were I, I'd have status in the Orders model (and I'd be calling it Order, not Orders) ALSO. That's not denormalization. status in History is the state when the history event happended. status in Orders is the current status. Bill On Tue, Oct 18, 2011 at 10:52 AM, Jack Morgan wrote: > I've got 2 tables that are related to each other. Orders and History. > Inside the History table is the 'status' column. Like so.. > > class Orders(models.Model): > 'order info' > > class History(models.Model): > timestamp = models.DateTimeField(auto_add_now = True) > order = models.ForeignKey(Orders) > user = models.ForeignKey(User) > comment = models.TextField() > status = models.CharField(max_length = 20) > > > I'm storing the status in the History table right now because I need to keep > track of who is moving the orders along. However, I need to do a lookup on > orders based on their current(most recent) status. For simplicities purpose > it seems a good idea would be to just put a status field in the Orders table > and update as the Order advances through the stages. However, that creates > duplicate data, which my client has expressed an extreme hatred for. > > What It's like to be able to do is something like: > o = Orders.filter(status = 'new') > > I'm not entirely sure how or when relationship managers are brought in, so I > was thinking something like this in the Orders model: > status = self.history_set.values('status').order_by('-id')[0]['status'] > > But that wouldn't let me do a lookup by state. > > What's a good Django way to handle this issue? > > Thanks! > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-users@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. > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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.
Doing a relationship lookup from within a model.
I've got 2 tables that are related to each other. Orders and History. Inside the History table is the 'status' column. Like so.. class Orders(models.Model): 'order info' class History(models.Model): timestamp = models.DateTimeField(auto_add_now = True) order = models.ForeignKey(Orders) user = models.ForeignKey(User) comment = models.TextField() status = models.CharField(max_length = 20) I'm storing the status in the History table right now because I need to keep track of who is moving the orders along. However, I need to do a lookup on orders based on their current(most recent) status. For simplicities purpose it seems a good idea would be to just put a status field in the Orders table and update as the Order advances through the stages. However, that creates duplicate data, which my client has expressed an extreme hatred for. What It's like to be able to do is something like: o = Orders.filter(status = 'new') I'm not entirely sure how or when relationship managers are brought in, so I was thinking something like this in the Orders model: status = self.history_set.values('status').order_by('-id')[0]['status'] But that wouldn't let me do a lookup by state. What's a good Django way to handle this issue? Thanks! -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@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.