forms.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. from __future__ import unicode_literals
  2. from django import forms
  3. from mezzanine.blog.models import BlogPost
  4. from mezzanine.core.models import CONTENT_STATUS_DRAFT
  5. # These fields need to be in the form, hidden, with default values,
  6. # since it posts to the blog post admin, which includes these fields
  7. # and will use empty values instead of the model defaults, without
  8. # these specified.
  9. hidden_field_defaults = ("status", "gen_description", "allow_comments")
  10. class BlogPostForm(forms.ModelForm):
  11. """
  12. Model form for ``BlogPost`` that provides the quick blog panel in the
  13. admin dashboard.
  14. """
  15. class Meta:
  16. model = BlogPost
  17. fields = ("title", "content") + hidden_field_defaults
  18. def __init__(self):
  19. initial = {}
  20. for field in hidden_field_defaults:
  21. initial[field] = BlogPost._meta.get_field(field).default
  22. initial["status"] = CONTENT_STATUS_DRAFT
  23. super(BlogPostForm, self).__init__(initial=initial)
  24. for field in hidden_field_defaults:
  25. self.fields[field].widget = forms.HiddenInput()