Skip to content

Instantly share code, notes, and snippets.

@cansadadeserfeliz
Last active August 29, 2015 13:57
Show Gist options
  • Save cansadadeserfeliz/9810462 to your computer and use it in GitHub Desktop.
Save cansadadeserfeliz/9810462 to your computer and use it in GitHub Desktop.
Django: add empty choice to СhoiceField for ModelForm
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