Wednesday, October 24, 2012

"calculated" filed in model


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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate