Created
February 25, 2017 12:53
-
-
Save hseritt/5a3f4409d4b95c616f8dac6143f0c01a to your computer and use it in GitHub Desktop.
Django form using super() and queryset
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
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