creating dropdown using three fields

I am trying to create a dropdown in my django form. I am new to django framework. While creating model class can we do something this or not,

/// model class

[NotMapped]
public string ddl_productdesc
{
    get
    {
        return string.Format("{1} --- {2}", ProductID, I_NUMBER, ProductDESC);
    }
}

In my table, there are around 30 columns from them I need three fields (ProductID, I_NUMBER, ProductDESC) for my dropdown

How can I do the same in django, the ID field used for data fetching and insertion and other two fields as dropdown? Thank You!

There’s a difference between a Model, which is your internal representation of your database, and a Form, which is your internal representation of a (or part of a) page.

What you’re looking to do here has nothing to do with your model. It’s associated with creating a widget on your page. In this particular case, you’re going to be creating a select widget for your field on the form, supplying choices that are dynamically created from your database.

This topic has been covered previously on the forum here - see Populate choice field from model with conditional query Django

1 Like