Filter in the form gives object number, not the name

``

user_management/forms.py

from django import forms
from .models import UserList

class UserForm(forms.ModelForm):
class Meta:
model = UserList
fields = ‘all

approval = forms.ModelChoiceField(
    queryset=UserList.objects.filter(designation__in=['Management', 'HOD']),
    label='Approved By'
)

def label_from_instance(self, obj):
    return f"{obj.fname} {obj.lname}"

``
gives UserList object I need obj.fname & obj.lname

1 Like

This needs to be defined on a field class, not the form class. See the last part this section of the docs Form fields | Django documentation | Django

from django.forms import ModelChoiceField


class MyModelChoiceField(ModelChoiceField):
    def label_from_instance(self, obj):
        return "My Object #%i" % obj.id

Then you need to use that custom field class on your form.

Thank you. I will try that.