开发者

Django admin site: how to compute field from multiple fields value?

开发者 https://www.devze.com 2023-01-20 04:05 出处:网络
I am wondering if is there a way to compute a field in the admin site based on a concatenation of multiple fields.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号