How to automatically choose model fields in django after calculation?

class Home(models.Model):
    home_type = models.CharField(max_length=255)
    home_square = models.PositiveIntegerField(default=0)

    def __str__(self):
        return self.home_type

Example output : Small home (type) = 60m2 (Home Square) // Medium home (type) = 90m2 (Home Square) // Big home (type) = 200m2 (Home Square)

class Property(models.Model):
    name = models.CharField(max_length=255)
    width = models.PositiveIntegerField(default=0)
    length = models.PositiveIntegerField(default=0)
    property_type = models.ForeignKey(Box,null=True, blank=True, on_delete=models.CASCADE)


    def __str__(self):`
        return self.property_name
    
    @property
    def property_square(self):
        property_square = self.product_length * self.product_width
        return property_square
        

How can I assign a property type from Home model automatically to Property model according to property_square after calculation?

if property_square < 60 than choose property type from Home model as a Small Home. if property_square >60 <90 than choose property type from Home model as a Medium Home.



Read more here: https://stackoverflow.com/questions/68469500/how-to-automatically-choose-model-fields-in-django-after-calculation

Content Attribution

This content was originally published by turgovic at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: