Last active
August 29, 2015 13:57
-
-
Save cansadadeserfeliz/9810462 to your computer and use it in GitHub Desktop.
Django: add empty choice to СhoiceField for ModelForm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
class Project(models.Model): | |
name = models.CharField(max_length=140) | |
parent = models.ForeignKey('self', blank=True, null=True) | |
class ProjectForm(ModelForm): | |
def __init__(self, *args, **kwargs): | |
super(ProjectForm, self).__init__(*args, **kwargs) | |
ordered = [] | |
// ... making ordered list ... | |
self.fields['parent'].choices = ordered | |
self.fields['parent'].choices.insert(0, ('', u'------')) | |
# You can import blanc field from: | |
# from django.db.models.fields import BLANK_CHOICE_DASH |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment