I have a form with data. The agegroup needs to be calcuated by other choosen values.
I have a model
sex_choices = [(‘w’, ‘female’), (‘m’, ‘male’)]
firstname = models.CharField(max_length=40)
lastname = models.CharField(max_length=40)
sex = models.CharField(max_length=1, choices=sex_choices)
year_of_birth = models.IntegerField(default=current_year)
and a form where the item agegroup needs to be calculated by the year_of_birth.
def save_model(self, request, obj, form, change): member_id = ??? Member.year_of_birth = ???
obj.agegroup = current_year - year_of_birth
super().save_model(request, obj, form, change)
Traditionally, when you define a reference as a foreign key, you name it relative to the class, you don’t name them with the “_id” suffix. See the Related objects reference.
When you reference some_instance.distance, you’re actually referencing the related instance of the Distance model, not the id field on it.
So, given some instance of a model Result named result, result.member is the relate Member instance. Therefore, result.member.id is the id field within that instance, and result.member.agegroup is the agegroup field.
Thank you, I think I understand that.
But I only have obj or form from the function: def save_model(self, request, obj, form, change):
So I still don´t know how to get the year_of_birth of the choosen member.