brechin / django-computed-property

Computed Property Fields for Django

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Trigger re-save on related model updates

brechin opened this issue · comments

Sometimes you want to store data that's based on a related model.

e.g. if you have the following model:

class Driver(models.Model):
    name = models.CharField(...)
    vehicle = models.ForeignKey(Vehicle)
    driver_bio = models.ComputedCharField(compute_from='vehicle_based_bio', ...)

    def vehicle_based_bio(self):
        return 'My name is {NAME} and I drive a {QUALITY} {COLOR} {MODEL}'.format(
            NAME=self.name,
            QUALITY=self.vehicle.quality,
            COLOR=self.vehicle.color,
            MODEL=self.vehicle.model
        )

... it would be great if the Driver object could be updated when the Vehicle is modified.

commented

I have implemented something similiar to your django app with updates across relations. You might want to have a look it here https://github.com/netzkolchose/django-computedfields.

@jerch Thanks, I will check it out!