Django admin site: how to compute field from multiple fields value?
I am wondering if is there a way to compute a field in the admin site based on a concatenation of multiple fields.
Basically I have a Product model with different fields associated to various attributes (colour, size, length etc).
I would like to compute the code value to be a concatenation of the values o开发者_StackOverflowf the various attribute fields like:
code = colour + "_" + size + "_" + length
There are a few ways to do this. I've done things like this in my models' clean
method:
def Product(models.Model)
# field definitions here
def clean(self):
self.code = self.colour + "_" + self.size + "_" + self.length
Doing it in the model layer (which will only work on versions of Django 1.2 and above) has the advantage that it'll be applied everywhere, not just where you use a particular form.
look at ModelAdmin.prepopulated_fields
精彩评论