[Answered ]-Get related objects

2๐Ÿ‘

โœ…

I think for both the scenarios you can use property decorator;

class User(AbstractBaseUser):
born = models.DateField(null=True, blank=True)
.....

@property
def age(self):
    '''Returns the age of this user'''
    if not self.born:
        return None
    today = date.today()
    return today.year - born.year - ((today.month, today.day) < (born.month, born.day))


@property
def tags(self):
    '''Returns the tags of this user'''
    return MeasurementResult.objects.filter(measurement_id__in=self.measurements.all().values_list('id', flat=True)).distinct().values_list('tag', flat=True)

Here you will get a list of tags

๐Ÿ‘คGeo Jacob

Leave a comment