views:

20

answers:

1

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 of the various attribute fields like:

code = colour + "_" + size + "_" + length
A: 

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.

Dominic Rodger
doesnt seem to work: it throws an error of the type "instance needs to have a primary key value before a many-to-many relationship can be used.". String concatenation also doesnt seem to work.
mαττjαĸøb