I want to add an extra button at submit_line.html
that does exactly the same thing as submit but it sets certain field to something else while sending post request to server.
Say I have a blog as model, I want to add "save as draft" as button.
My submit_line.html
looks like this:
{% load i18n admin_urls %}
<div class="submit-row">
{% if show_save %}<input type="submit" value="{% trans 'Save' %}" class="default" name="_save" />{% endif %}
{% if show_save_as_draft %}<input type="submit" value="save_as_draft" class="default" name="_save" />{% endif %}
{% if show_delete_link %}
{% url opts|admin_urlname:'delete' original.pk|admin_urlquote as delete_url %}
<p class="deletelink-box"><a href="{% add_preserved_filters delete_url %}" class="deletelink">{% trans "Delete" %}</a></p>
{% endif %}
{% if show_save_as_new %}<input type="submit" value="{% trans 'Save as new' %}" name="_saveasnew" />{% endif %}
{% if show_save_and_add_another %}<input type="submit" value="{% trans 'Save and add another' %}" name="_addanother" />{% endif %}
{% if show_save_and_continue %}<input type="submit" value="{% trans 'Save and continue editing' %}" name="_continue" />{% endif %}
</div>
Now I don't understand how it is making this request, so I can't modify it, so please help me regarding this.
First, give your button a unique
name
:Then, in your
ModelAdmin
you can override thesave_model()
method:One important thing is that
save_model()
has to save the instance. This means if the method or one of itsif/else
branches doesn't callsuper
you need to make sure to callobj.save()
somewhere.There are many more possibilities to customize the workflow based on what button has been clicked. The gist is that this information is available in the
request
instance, which is accessible in most relatedModelAdmin
methods.E.g. another interesting use case is redirecting the user to somewhere else after hitting the Save as draft button, which is possible by overriding
response_add()
andresponse_change()
.