Wow, that is very cool. I implemented it and it works! Django is
definitely a very impressive framework.

Thanks for your help!
Kevin

On Feb 6, 10:31 am, Alex Gaynor <alex.gay...@gmail.com> wrote:
> On Fri, Feb 6, 2009 at 12:58 PM, Kevin Audleman 
> <kevin.audle...@gmail.com>wrote:
>
>
>
>
>
> > Thanks Alex!
>
> > My Project class has the field
>
> > client = models.ForeignKey(Client')
>
> > Would I use this in my save() method to reference the parent Client?
> > For instance
>
> > class Project:
> >    def save():
> >        self.hourly_rate = client.hourly_rate
> >        ...
>
> > Or is there some other way of working across the relationship?
>
> > Cheers,
> > Kevin
>
> > On Feb 6, 9:46 am, Alex Gaynor <alex.gay...@gmail.com> wrote:
> > > On Fri, Feb 6, 2009 at 12:43 PM, Kevin Audleman <
> > kevin.audle...@gmail.com>wrote:
>
> > > > I'm building a simple time tracker. It has a table Clients and a table
> > > > Projects and there is a one-to-many relationship between them. I  have
> > > > a field called hourly_rate that is in both tables. The reasoning is
> > > > that in general the hourly rate is the same across projects based on
> > > > the client, but if at some point the hourly_rate goes up, I need to
> > > > keep track of the historical rate I charged on Projects. What I would
> > > > like to do is, on creation of a new Project, automatically populate
> > > > its hourly_rate field with the value from Clients. Is this possible to
> > > > set up in django? I'm used to Filemaker where I have the ability to
> > > > auto-populate fields on creation through relationships, but django is
> > > > a new world unto me.
>
> > > > Thanks for your help,
> > > > Kevin Audleman
>
> > > Yep this is possible, basically what you're going to want to do is
> > override
> > > the save() method on a Project so that when it gets created it just
> > copies
> > > it's Clients hourly pay, you can look for other examples of using this
> > > technique to accomplish other things(such as automatically setting the
> > > creation date on a field).
>
> > > Alex
>
> > > --
> > > "I disapprove of what you say, but I will defend to the death your right
> > to
> > > say it." --Voltaire
> > > "The people's good is the highest law."--Cicero
>
> It would look something like this:
>
> class Project(models.Model):
>    def save(self, *args, **kwargs):
>        if not self.id:
>            self.hourly_rate = self.client.hourly_rate
>        super(Project, self).save(*args, **kwargs)
>
> that way it only sets the hourly rate the first time you save a project, in
> case you edit it later you don't want the hourly rate to change.
>
> Alex
> --
> "I disapprove of what you say, but I will defend to the death your right to
> say it." --Voltaire
> "The people's good is the highest law."--Cicero
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to