Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@hseritt
Created February 25, 2017 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hseritt/5a3f4409d4b95c616f8dac6143f0c01a to your computer and use it in GitHub Desktop.
Save hseritt/5a3f4409d4b95c616f8dac6143f0c01a to your computer and use it in GitHub Desktop.
Django form using super() and queryset
class ArticleUpdateForm(ModelForm):
"""
Form for updating an article.
"""
def __init__(self, *args, **kwargs):
super(ArticleUpdateForm, self).__init__(*args, **kwargs)
self.fields['attachments'] = forms.ModelMultipleChoiceField(
queryset=self.instance.attachments.all(), required=False
)
self.fields['versioning_mode'] = forms.ChoiceField(
label='Save new versions as',
choices=(
('Minor', 'Minor'),
('Major', 'Major'),
),
)
self.fields['content'] = forms.CharField(
widget=forms.Textarea(attrs={'class': 'content_textarea'}),
initial=self.instance.get_latest_content(),
)
self.fields['project'] = forms.ModelChoiceField(
queryset=Project.objects.filter(allow_articles=True),
required=False
)
class Meta:
model = Article
widgets = {'description': forms.Textarea(attrs={'rows':6, 'cols':89}),}
fields = [
'title', 'audience', 'is_published', 'project',
'description', 'attachments',
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment