Doing a relationship lookup from within a model.

2011-10-18 Thread Jack Morgan
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.



Re: Getting a count of a RawQuerySet object

2011-09-21 Thread Jack Morgan
Hi, thanks!
The issue I'm getting here is that the RawQuerySet object does not have the
.count() method so it just gives an error.
I also just checked that it does not have the 'all()' method either.


On Wed, Sep 21, 2011 at 9:23 AM,  wrote:

> Let's say you have a model Utility, you can do
> Utility.objects.all().count()
> Hope that helps.
> Regards.
> Sent from my BlackBerry wireless device from MTN
>
> -Original Message-
> From: Jack 
> Sender: django-users@googlegroups.com
> Date: Wed, 21 Sep 2011 09:10:49
> To: Django users
> Reply-To: django-users@googlegroups.com
> Subject: Getting a count of a RawQuerySet object
>
> Hello everyone, I'm quite new to django and python in general so I'm
> sure there's just something I missed in my searching to answer this.
> But...
>
> Is there a way to count the number of results returned in the
> RawQuerySet option?
>
> I've tried the len() function and I've search the django documentation
> with no results. So, any help would be very welcomed.
>
> 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.
>
>

-- 
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.