Admin Site Foreign Key Field Drop-Down List Multiple Columns

Hello and Greets,

I am very new to Django with moderate Python experience, and I probably should not be asking this at this stage of my development, but I have been searching on how to do what I want to do and could not find it, so I decided to post my question here. I want to learn the full workings of this wonderful framework while applying it to SEVERAL conceived uses along the way, so while I progress through a few courses on the basics, I am also creating working applications for my own use. This is the reason I am asking about this functionality now.

My parallel application/site is a simple budget manager. On the admin site, as you all know, I can create transactions, accounts, etc. using the models I created. I am using a one-to-many relationship in a field using ForeignKey, between transactions and accounts, and it works great! The problem is… in the drop-down list, I want to show more than one column. Each account has 3 fields, a full long name, a short name, and an acronym. I want to select from a list of acronyms, but also show the short account name beside it. I am sure this question qualifies as RUNNING before learning to crawl, but I could not resist asking now. If that offends, I certainly apologize!!

I am creating this application beside the locallibrary course site in this tutorial at https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Home_page. I am at Django Tutorial Part 5: Creating our home page so I am sure I will come across this functionality eventually. And not being able to do what I mentioned is not a real big deal, I just like to explore the possibilities.

I never used a framework before. I was in the middle of writing my own using ajax, jquery, and php when it became considerably convoluted and I certainly lost my way. I am so impressed with Python, and now Django that I cannot wait to see what this can do for me and my needs. As an embedded systems designer, I need database supported sites for inventory, designs, budgeting, and documentation. I am hoping to do all of these with Django, Python, Conda, and vscode.

Any suggestions of further reading and research would be appreciated.

Thank you for your time!

Hop of HopWorks Embedded Imagination

To clarify, are you saying that within a drop down, you want the list of items to show more than one field?

If so, that’s easy. The choices for a select field is a list of 2-tuples where the first value is the value returned for the selection and the second is the value displayed that field. You can construct that second value from data in models.

1 Like

Quick fix

Django admin’s ForeignKey uses the model __str__ to represent the instance in the select. Reference (Search for special cases)

class Account(models.Model):
    # fields
    full_name
    short_name
    acronym

    def __str__(self):
        return f"{self.acronym} - {self.short_name}"

:warning: The __str__ method is used in many other places.

Specific solution

Override the form field for the ForeignKey

1 Like

I am not sure which provided the solution yet, but both of your replies were excellent at pointing me in the right direction. marcorichetta, Your solution is interesting because I had not considered returning a formatted value from str to include both fields. Very nice! I really wish I had thought of that.

KenWhitesell, I am digging deeper in the info you provided to see how a tuple in choices relates to what is returned in str.

Thank you gentlemen for linking info with your replies! I do not want it done for me as a goal, although that helps. I also want to learn why and I appreciate it! THANK YOU!

The quick fix that worked was from marcorichetta
image

For Ken’s, I’ll have to dig a bit deeper to see how this helps my main-site’s presentation of the form.

Again, I appreciate the info and push in the right direction, thank you!!

While @marcorichetta’s solution works, it “ties up” the __str__ method, which as he points out, is used in multiple places. We use it for other purposes, so it’s not available for us to format selection lists.

The specific mechanics of that is documented in the ModelChoiceFields section of the Form fields docs (see the last paragraph). That paragraph also talks about using the label_from_instance method where you can write your own function to generate entries without using __str__

2 Likes

I see your point, actually stated in both of the replies. I am not sure it is appropriate for me at my stage of development to detour into this, but I will. At least to make some notes as it will apply later. I do not want to implement quick fixes that might hurt functionality later. It’s nice to know both routes however. Thanks again!