1👍
✅
have you considered django mptt?
it adds a whole bunch of helpers for dealing with tree data, including nice nested widgets
otherwise, i think i’ve used something like this in the past
(copied form old codebase, so may need tweaking to work. also, think it was written for django 1.2 so probably needs updating)
class MyChoiceField(forms.ModelChoiceField):
def label_from_instance(self, obj):
return my_fun(obj)
class MyModelAdmin(admin.ModelAdmin):
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "my_field":
kwargs["form_class"] = MyChoiceField
return db_field.formfield(**kwargs)
return super(MyModelAdmin, self).formfield_for_foreignkey(
db_field, request, **kwargs)
Source:stackexchange.com