Hey; i have a model using user as a forgignKey. and in the admin, i need it show as full name instread of username. then i applied
class CollaboratorInLine(admin.StackedInline):
model = Collaborator
#list_display = ('collaborator__collaborator_first_name')
extra = 0
form =CollaboratorForm
#raw_id_fields=("collaborator",)
def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
field = super(CollaboratorInLine, self).formfield_for_foreignkey(
db_field, request, **kwargs)
if db_field.rel.to == User:
field.label_from_instance = self.get_user_label
return field
def formfield_for_manytomany(self, db_field, request=None, **kwargs):
field = super(CollaboratorInLine, self).formfield_for_manytomany(
db_field, request, **kwargs)
if db_field.rel.to == User:
field.label_from_instance = self.get_user_label
return field
def get_user_label(self, user):
name = user.get_full_name()
return name
it works fine as a dropdown list with fullnames. then i want to sort them in alphabetical order. then i appplied
class CollaboratorForm(forms.ModelForm):
collaborator = forms.ModelChoiceField(queryset=User.objects.order_by('first_name'))
#assert False,collaborator
class Meta:
model = Collaborator
as this applied. the full names becomes username again. if i remove it, it becomes full names. so how can i keep both features?
thanks