0

I have a problem with re-assigning some var in my model, I have a model like this:

class ExpireTime(models.Model):
     """Expire time fields and methods in abstract mode"""
    
     def expire_time():
         return create_expire_time(seconds=10)

     expire = models.DateTimeField(default=expire_time)

     def is_expired(self) -> bool:
         return self.expire < timezone.now()

     class Meta:
         abstract = True

but in the different models that used this abstract model, I need the use different expiration times in the exprie_time:

def expire_time():
         return create_expire_time(seconds=10)

so I am trying to overwrite this method in the model that was inherited from ExpireTime but it has no effect.

so how can I solve this situation? Should I not use the abstract model in this situation?

Update:

the model that was inherited from ExpireTime :

class TempLink(ExpireTime, models.Model):
    """Temp link for provide some files with security"""

    # Needs 10 sec expiration time 

    link = models.UUIDField(
        primary_key=True, default=uuid4, editable=False, unique=True
    )
    ip = models.GenericIPAddressField()
    file = models.FileField()

    def is_valid_ip(self, ip: models.GenericIPAddressField) -> bool:
        return ip == self.ip

class UserConfirm(ExpireTime, models.Model):
    """confirm user activate wiht random code"""

    # needs 120 sec expiration time 

    LENGTH_CODE: int = 5

    # Generate Random Code Between 0 to 9
    def generate_code() -> str:
        code = "".join(
            [str(random.randint(0, 9)) for _ in range(UserConfirm.LENGTH_CODE)]
        )
        return code

    user = models.OneToOneField(NewUser, on_delete=models.CASCADE)
    code = models.CharField(max_length=LENGTH_CODE, default=generate_code, unique=True)
    token = models.UUIDField(default=uuid4, unique=True)

    def is_valid_code(self, input_code):
        return input_code == self.code
Nova
  • 413
  • 4
  • 11
  • 1
    Can you share that another model? – Sunderam Dubey Jul 08 '22 at 13:50
  • If this is an abstract base model you can override the field's definition. – Bernhard Vallant Jul 08 '22 at 13:54
  • You write `DateTimeField(default=expire_time)` when this gets run it passes that specific function to the `DateTimeField` classes `__init__` class. No overriding is going to work in this case. – Abdul Aziz Barkat Jul 08 '22 at 13:58
  • @SunderamDubey that model only using it but with the different goal I have a TempLink model that needs expiration time with 10 sec only, but another model needs 120 sec or higher, I need be ability to set expire time form inherited models – Nova Jul 08 '22 at 14:00
  • yes @AbdulAzizBarkat is main the reason why I ask questions about it, I can't find a good solution way to fix it – Nova Jul 08 '22 at 14:05
  • Does this answer your question? [How to override the default value of a Model Field from an Abstract Base Class](https://stackoverflow.com/questions/6377631/how-to-override-the-default-value-of-a-model-field-from-an-abstract-base-class) or as @BernhardVallant mentions above you can simply override the entire field definition. – Abdul Aziz Barkat Jul 08 '22 at 14:06

1 Answers1

0

you can always do it on save:

class ExpireTime(models.Model):
    default_expired = 10

    expire = models.DateTimeField(null=True, blank=true)

    class Meta:
        abstract = True

    def save(self, *args, **kwargs):
      if not self.expire:
        self.expire = create_expire_time(seconds=self.default_expired)
      super().save(*args, **kwargs)


class OtherExpireTime(ExpireTime):
    default_expired = 20
Maxim Danilov
  • 2,472
  • 1
  • 3
  • 8