UpdateView with additional fields in Django

I'm building an app where users can submit a ThesisLink, which contains metadata of their MSc or PhD thesis. Before a thesis link is published, a vetting editor must have the possibility to change fields (for example, in the case of a broken link) or outright reject the thesis link. Submitters should be mailed when their thesis link is accepted, accepted with certain changes, or rejected.

I came to the conclusion that I want some sort of UpdateView, where all the fields of the model are already filled out, and ready to be edited by a vetting editor. But I also want fields that are not on the model, like refusal_reason, or editor_comment. And I want to notify users by mail when a change happens.

How to extend the update view to do this? Or should I abandon the UpdateView altogether and build something on top of FormView?

This is what I have so far:

# urls.py
urlpatterns = [
        url(r'^vet_thesislink/(?P<pk>[0-9]+)/$', views.VetThesisLink.as_view(), name='vet_thesislink')
]



# views.py
@method_decorator(permission_required(
    'scipost.can_vet_thesislink_requests', raise_exception=True),     name='dispatch')
class VetThesisLink(UpdateView):
    model = ThesisLink
    fields = ['type', 'discipline', 'domain', 'subject_area',
          'title', 'author', 'supervisor', 'institution',
          'defense_date', 'pub_link', 'abstract']
    template_name = "theses/vet_thesislink.html"

And in the template:

# templates/theses/vet_thesislink.html
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Update" />
</form>

Upvotes: 1

Views: 1784

Answers (1)

Udi
Udi

Reputation: 30492

You will need to create a custom form using ModelForm with additional non-model fields, and use it in UpdateView using the form_class attribute.

Upvotes: 3

Related Questions