I have a model in Django that looks like this:
class Project(models.Model):
name = models.CharField(max_length=140)
parent = models.ForeignKey('self', blank=True, null=True)
The goal is to make a form for this model with select widget for the parent field that will represent the hierarchical structure of projects. I tried to redefine choices pairs for this field by sorting queryset and adding indent in front of the label for each choice. But when I make a list of pairs (project.id, project.name) for the form
class ProjectForm(ModelForm):
def __init__(self, *args, **kwargs):
super(ProjectForm, self).__init__(*args, **kwargs)
ordered = []
// ... making ordered list ...
self.fields['parent'].choices = ordered
the default empty choice for this field gets lost. Does anyone know how to get it back?
Thanks in advance.
In
__init__, add: