add element in admin with foreign keys and do not show object (x)

Hi,
I have a Class result with foreign keys.
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_id = models.ForeignKey(Agegroup, on_delete=models.CASCADE)
result_value = models.IntegerField()

and a class distance
class Distance(models.Model):
category_choices = [(‘d’, ‘distance’), (‘t’, ‘time’)]
sort = models.IntegerField(unique=True)
min = models.IntegerField()
max = models.IntegerField()
name = models.CharField(max_length=40, unique=True)
category = models.CharField(choices=category_choices, max_length=1)

If I choose add result I see only Distance object (x)
add

How can I change that list to the value of “name” or even better a “name, category”?
I there something like “list_display”?

Regards,
Andreas

One way to do it is to define a __str__ method on your model:

class Distance(models.Model):
    ...
    def __str__(self):
        return f"{self.name} {self.category}"

Alternatively you could customize the form for the admin and override the fields so that you can set the labels.

1 Like

Great thanks.
That was easy.