'Changing the format of a field depending on another fiel in the same model
Using Django I'm trying to change the format of a field depending on another field in the same model.
I have a group of persons who give me a detail which could be in two different formats: time, or decimal. I would like to have all the results in the same table. Is that possible?
This is what I tried in models.py:
class Time_manager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(result_format='T')
class Kilo_manager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(result_format='K')
class Guest(models.Model):
name = models.CharField(max_length=30)
result_format=models.CharField(max_length=5, default="K")
result= models.DecimalField(max_digits=6,decimal_places=2)
class Guest_time(Guest):
objects=Time_manager()
class Meta:
proxy = True
def formato_resultado(self):
self.resultado=models.DurationField()
But when I try to add a new Guest_time I'm not able to do it. I get an error asking me to introduce a decimal data.
Is there a way of doing this using the same table?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
