Search code examples
pythondjangoormmodels

Django: Setting model's properties dynamically


Is it possible to define the property of a model dynamically since 'self' is not defined?

I have two models, Slider and SliderImage. I would like to define the property in SliderImage according to the value of a property defined in the related model Slider.

class Slider(models.Model):
    width = models.SmallIntegerField(blank=True,null=False)
    height = models.SmallIntegerField(blank=True,null=False)

class SliderImagen(models.Model):
    imagen = ProcessedImageField(
        processors[ResizeToFill( **self.slider.width**,**self.slider.height**)])
    slider = models.ForeignKey(
        'Slider',
        on_delete=models.CASCADE,db_column='slider',
        related_name='imagenes')

Solution

  • I think looking into https://docs.djangoproject.com/en/2.0/howto/custom-model-fields/#converting-values-to-python-objects would help you.