Skip to content Skip to sidebar Skip to footer

Extra Parameter For Django Models

With Django models, I want to achieve this: class Foo(models.Model): name = models.CharField(max_length=50) #wrapping the save function, including extra tasks def save

Solution 1:

You can define non-persistent fields in your model.

classFoo(models.Model):
    name = models.CharField(max_length=50)
    extra_param = Falsedefsave(self, *args, **kwargs):
    ... 
    print self.extra_param

Alternatively, you can do:

Foo(name="Bill Gates").save(extra_param=True)

defsave(self, *args, **kwargs):
    ... 
    print kwargs["extra_param"]

Post a Comment for "Extra Parameter For Django Models"