Re: Query field across multiple django models

2010-10-26 Thread Ed
Do you have further detail on how this looks?  I was intending to use
forms.changed_data, but I would much rather get an automatic list of
changed fields by comparing the new submission to the initial state.
How do I compare the initial to the post within a pre_save function?


On Oct 26, 12:18 pm, bruno desthuilliers
 wrote:
> On 26 oct, 15:48, Ed  wrote:
>
> > I do like this a lot.  If I use post_save, is there a way to grab the
> > field that is altered?
>
> Nope, you have to use "pre_save" for this, load another (yet
> unmodified) copy of your model instance, and diff both.
>
> > I know the admin page seems to be able to
> > determine the changed field. . .
>
> ModelForms do keep a copy of the initial state so they can tell what
> went modified.

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



Re: Query field across multiple django models

2010-10-26 Thread bruno desthuilliers
On 26 oct, 15:48, Ed  wrote:
> I do like this a lot.  If I use post_save, is there a way to grab the
> field that is altered?

Nope, you have to use "pre_save" for this, load another (yet
unmodified) copy of your model instance, and diff both.

> I know the admin page seems to be able to
> determine the changed field. . .


ModelForms do keep a copy of the initial state so they can tell what
went modified.

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



Re: Query field across multiple django models

2010-10-26 Thread Scott Gould
> Second, I'm not sure I understood the last part about getting the
> field within each model.

Sorry, I misread, thinking you were talking about having different
parameters of each model being responsible for what counted as
"latest". Bruno's solution looks good to me.

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



Re: Query field across multiple django models

2010-10-26 Thread Ed
I do like this a lot.  If I use post_save, is there a way to grab the
field that is altered?  I know the admin page seems to be able to
determine the changed field. . .

On Oct 26, 9:41 am, bruno desthuilliers
 wrote:
> On 26 oct, 13:25, Ed  wrote:
>
>
>
>
>
>
>
>
>
> > I want to create a "What's New" section that lists all of the database
> > changes in the last day. I've added an "updated" field to my models:
>
> > class Film(models.Model):
> >    .
> >    .
> >    .
> >    updated = models.DateTimeField(auto_now=True)
>
> > class Actor(models.Model):
> >    .
> >    .
> >    .
> >    updated = models.DateTimeField(auto_now=True)
>
> > Now I want to query across all of my models to get a date-sorted list
> > of the most recent changes. How do I query the "updated" field across
> > multiple models? Is this the most efficient way to achieve the primary
> > purpose?
>
> > What if I wanted to be more specific and list the actual field that
> > was altered within each model?
>
> Another solution is to have a specific "LastChange" model with a
> GenericForeignKey on your other models (the ones you want to monitor)
> and hook into the appropriate signal (models.signals.pre_save or
> models.signals.post_save look like a good start) to feed it with
> relevant data.
>
> The neat points are that:
>
> 1/ you can monitor just any model, without having to add special
> fields
> 2/ you only have one table to query to build your "what's new" section
> (depending on what infos you store in LastChange, you may not need to
> get at the changed object at all)
>
> HTH

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



Re: Query field across multiple django models

2010-10-26 Thread Ed
If there is no simpler way to do this, then I'm fine with this
method.  2 questions though.

Is there a concern about the size of the query required for the
big_honking_list?  I would probably be pulling a much larger dataset
than I need and can only truncate it after it's sorted by date.

Second, I'm not sure I understood the last part about getting the
field within each model.  So my example:

class Film(models.Model):
   title = models.CharField(max_length=50, unique = True)
   year = models.IntegerField()
   length = models.IntegerField()
   updated = models.DateTimeField(auto_now=True)

If I updated length, saved, then updated year, then saved, could I get
a list that would say:

What's new
Film Length: updated 2:15am
Film Year: updated 2:20 am
etc.

On Oct 26, 7:44 am, Scott Gould  wrote:
> Maybe -- in fact, almost certainly not -- the best way, but this is
> how I do that kind of thing:
>
>         a_queryset = ModelA.objects.all()
>         another_queryset = ModelB.objects.filter.(by_something=True)
>         yet_another_queryset =
> ModelC.objects.exclude(by_something_else=False)
>
>         from itertools import chain
>         big_honking_list = list(chain(a_queryset, another_queryset,
> yet_another_queryset))
>         big_honking_list.sort(key=lambda x: x.updated)
>         big_honking_list.reverse()
>
> As to your last question, that is easy if you're doing a list.sort()
> -- just expose a method on each model that returns a sortable value
> that is consistent across all the models. (Or write a dedicated
> function to use instead of the lambda above, whichever makes more
> sense.)
>
> On Oct 26, 7:25 am, Ed  wrote:
>
>
>
>
>
>
>
> > I want to create a "What's New" section that lists all of the database
> > changes in the last day. I've added an "updated" field to my models:
>
> > class Film(models.Model):
> >    .
> >    .
> >    .
> >    updated = models.DateTimeField(auto_now=True)
>
> > class Actor(models.Model):
> >    .
> >    .
> >    .
> >    updated = models.DateTimeField(auto_now=True)
>
> > Now I want to query across all of my models to get a date-sorted list
> > of the most recent changes. How do I query the "updated" field across
> > multiple models? Is this the most efficient way to achieve the primary
> > purpose?
>
> > What if I wanted to be more specific and list the actual field that
> > was altered within each model?

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



Re: Query field across multiple django models

2010-10-26 Thread bruno desthuilliers
On 26 oct, 13:25, Ed  wrote:
> I want to create a "What's New" section that lists all of the database
> changes in the last day. I've added an "updated" field to my models:
>
> class Film(models.Model):
>    .
>    .
>    .
>    updated = models.DateTimeField(auto_now=True)
>
> class Actor(models.Model):
>    .
>    .
>    .
>    updated = models.DateTimeField(auto_now=True)
>
> Now I want to query across all of my models to get a date-sorted list
> of the most recent changes. How do I query the "updated" field across
> multiple models? Is this the most efficient way to achieve the primary
> purpose?
>
> What if I wanted to be more specific and list the actual field that
> was altered within each model?

Another solution is to have a specific "LastChange" model with a
GenericForeignKey on your other models (the ones you want to monitor)
and hook into the appropriate signal (models.signals.pre_save or
models.signals.post_save look like a good start) to feed it with
relevant data.

The neat points are that:

1/ you can monitor just any model, without having to add special
fields
2/ you only have one table to query to build your "what's new" section
(depending on what infos you store in LastChange, you may not need to
get at the changed object at all)

HTH

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



Re: Query field across multiple django models

2010-10-26 Thread Scott Gould
Maybe -- in fact, almost certainly not -- the best way, but this is
how I do that kind of thing:

a_queryset = ModelA.objects.all()
another_queryset = ModelB.objects.filter.(by_something=True)
yet_another_queryset =
ModelC.objects.exclude(by_something_else=False)

from itertools import chain
big_honking_list = list(chain(a_queryset, another_queryset,
yet_another_queryset))
big_honking_list.sort(key=lambda x: x.updated)
big_honking_list.reverse()

As to your last question, that is easy if you're doing a list.sort()
-- just expose a method on each model that returns a sortable value
that is consistent across all the models. (Or write a dedicated
function to use instead of the lambda above, whichever makes more
sense.)

On Oct 26, 7:25 am, Ed  wrote:
> I want to create a "What's New" section that lists all of the database
> changes in the last day. I've added an "updated" field to my models:
>
> class Film(models.Model):
>    .
>    .
>    .
>    updated = models.DateTimeField(auto_now=True)
>
> class Actor(models.Model):
>    .
>    .
>    .
>    updated = models.DateTimeField(auto_now=True)
>
> Now I want to query across all of my models to get a date-sorted list
> of the most recent changes. How do I query the "updated" field across
> multiple models? Is this the most efficient way to achieve the primary
> purpose?
>
> What if I wanted to be more specific and list the actual field that
> was altered within each model?

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



Query field across multiple django models

2010-10-26 Thread Ed
I want to create a "What's New" section that lists all of the database
changes in the last day. I've added an "updated" field to my models:

class Film(models.Model):
   .
   .
   .
   updated = models.DateTimeField(auto_now=True)

class Actor(models.Model):
   .
   .
   .
   updated = models.DateTimeField(auto_now=True)

Now I want to query across all of my models to get a date-sorted list
of the most recent changes. How do I query the "updated" field across
multiple models? Is this the most efficient way to achieve the primary
purpose?

What if I wanted to be more specific and list the actual field that
was altered within each model?

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