On Oct 25, 12:26 pm, Russell Keith-Magee <russ...@keith-magee.com> wrote: > Hi Mike, > > Easily done
Just as you said. Works like a charm. And as an added benefit, I understand what the code's doing. Caveats noted. Thanks a bunch. -Mike. > , and no need to use signals or raw SQL: just override the save > method on your model so that every time you call save, the value in the baz > column is updated. > > class Foo(models.Model): > bar = models.CharField() > baz = models.CharField() > > def save(self, *args, **kwargs): > self.baz = function(self.bar) > return super(Foo, self).save(*args, **kwargs) > > As a warning, there are some times when this won't work. For example, baz > won't be updated if you use a .update() call on a queryset, and it won't be > updated if you load a fixture or perform a "raw" save. However, this would > also be true of a signal based approach. For most simple uses, it should be > fine. > > Yours, > Russ Magee %-) > > > > > > > > On Thu, Oct 25, 2012 at 10:12 AM, Mike Burr <ad...@unintuitive.org> wrote: > > > I would like to accomplish the following: > > > class Foo(models.Model): > > bar = models.CharField() > > baz = function(self.bar) > > > Of course this doesn't work (at least as I want it to), but I'm guessing > > that most humans will know what I'm trying to do. What I want is: > > > 1) User supplies value for 'bar' > > 2) User clicks 'Save' > > 3) baz column gets value returned by 'function' (which will be a large > > string) > > > I don't care if 'baz' shows up on the Admin form, so long as after saving > > the calculated value shows up. I could probably do this with a DB trigger, > > but I'd like to do it the Django way. A trigger would also not be > > RDBMS-neutral I considered using signals, but: > > > 1) I don't think I can use pre_save, since I don't have a row yet. > > 2) I don't think I can use post_save because I don't know of a way to > > identify the right row. Also, post_save would make the transaction > > non-atomic (right?) If "function" threw an exception for example, the row > > would still get saved. > > > If I do use a signal, I'd also like to avoid using raw SQL if possible. > > I'd have to hard-code table, column names, which seems ugly to me. I just > > know there's a quick, elegant way of doing this. I'm just not Django savvy > > enough yet. > > > Thank you! > > > -- > > You received this message because you are subscribed to the Google Groups > > "Django users" group. > > To view this discussion on the web visit > >https://groups.google.com/d/msg/django-users/-/KC16U1q7p4AJ. > > 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.