admin form get id of foreign key

I have a form with data. The agegroup needs to be calcuated by other choosen values.

I have a model
class Member(models.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)

class Result(models.Model):
    distance_id = models.ForeignKey(Distance, on_delete=models.CASCADE)
    event_id = models.ForeignKey(Event, on_delete=models.CASCADE)
    member_id = models.ForeignKey(Member, on_delete=models.CASCADE)
    agegroup = models.CharField(max_length=3)
    result_value = models.IntegerField()

and a form where the item agegroup needs to be calculated by the year_of_birth.
class ResultAdmin(admin.ModelAdmin):
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)

How do I get the member_id of the chosen member?

Thank you for help

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, 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.

Review the save_model documentation and what you get passed in the method call.

Meamwhile I found a solution:
def save_model(self, request, obj, form, change):
member_data = str(form.cleaned_data[‘member_id’]).split(" “)
year_of_birth = member_data[3]
event_data = str(form.cleaned_data[‘event_id’]).split(” “)
date = event_data[0]
year_of_event = date.split(”-")[0]
age = int(year_of_event) - int(year_of_birth)
obj_agegroup = get_object_or_404(Agegroup, age=age)
agegroup_id = obj_agegroup.age
if sex == ‘m’:
agegroup = obj_agegroup.agegroupm
agegroup = obj_agegroup.agegroupw
obj.agegroup = agegroup
super().save_model(request, obj, form, change)

I don´t know if that a good solution, but it is working.