Custom display values in drop downs for TabularInline

I have a TabularInline that includes the CarType and Car models:

class Car(models.Model):
    name = models.CharField(max_length=100)
    type = models.ManyToMany(CarTypes, through="CarTypeWithCar"))
    manufacturer = models.ForeignKey(Manfacturer, on_delete=models.PROTECT)

    def __str__(self):
        return self.name

...

CarTypeWithCar(models.Model):
    car_type = models.ForeignKey(
        CarType, on_delete=models.CASCADE
    )
    car = models.ForeignKey(
        Car, on_delete=models.CASCADE
    )

...

class CarTypeWithCarAdminInline(admin.TabularInline):
    model = Car.type.through
    fields = ["car_type", "car"]

The problem is, when I click the select dropdown for the car field in my Inline, it lists just the name of the car (which can be a little confusing for a big list). I would like to customize the values in the car select to display something like f"{self.car.manufacturer} - {self.car}" instead of just the __str__ of the Car model. I really do not want to change the __str__, since I am using that in other places in my app.

I’ve wrestled with this the entire evening, and I’m still not sure how to make it happen. Thank you in advance for any assistance!

Hey there!
Maybe you can do this using formfield_for_foreignkey, just after this section, also there’s two anothers, maybe that fit into your use case.

1 Like

I’m not quite understanding what you’re trying to achieve here.

Typically, an AdminInline is used to allow for editing a second, related model, relative to the base model of that admin page.

For example, if this is being used in the admin page for Car, you could use the AdminInline to allow for the editing of related CarTypes.

It appears that you’re working in that direction - what confuses me is the presence of the car field in the fields list of CarTypeWithCarAdminInline - or why you’re creating an inline for the join table instead of CarType. (See Working with many-to-many models. )

Aside from that, there are ways to customize what shows up in the choices for a form field. TabularInline is a subclass of InlineModelAdmin. InlineModelAdmin has a variety of options including specifying the form class - which could be a custom form of your creation, containing the custom choices.

1 Like

Ken, thank you for your help and your patience with my confusing example! Customizing the values choices in the form is definitely what I want to do. I will try subclassing InlineModelAdmin.InlineModelAdmin and modifying the choices in a custom form. The models I’m actually using in my application are really large and thus not suitable for a forum post, but my contrived example seemingly wasn’t any better. :face_with_diagonal_mouth: Next time I will build a little app for an example instead.

@leandrodesouzadev thanks for the interesting link! Just a couple of items below your link, I noticed ModelAdmin.formfield_for_choice_field, which has some interesting logic for detecting my field. Thanks for the cool link!