0

I would like to customize the admin poll functionality in Django. Is it possible to override or add to existing functionality of admin site?

For example in the screenshot shown, I would like to add different functionality than what it automatically does when I click on "SAVE". I am new to Django and html, a brief procedure is greatly appreciated. enter image description here

Sandeep Thota
  • 135
  • 1
  • 11

1 Answers1

0

The admin's save method exist to save the model instance to the database.

If you like to add extra functionality to the admin's save method, simply override the method inside the ModelAdmin class:

class MyModelAdmin(admin.ModelAdmin):

    fields = [...]
    list_filter = [...]
    # etc.

    def save_model(self, request, obj, form, change):
        # do stuff here 

        # and don't forget to call super's save method
        super().save_model(request, obj, form, change)
nik_m
  • 11,825
  • 4
  • 43
  • 57
  • Thanks a lot for that quick response. Is it possible to access content I entered in the ballot creation page from this function? For example I want to access "Ballot Text" or "Ballot Address". Is it possible to access it from this save method? – Sandeep Thota Mar 25 '17 at 20:35
  • Of course you can. Just access `obj.ballot_text` or `obj.ballot_address`. – nik_m Mar 26 '17 at 06:39
  • thanks again. How I can access variable number of choices? – Sandeep Thota Mar 26 '17 at 15:01
  • What do you mean by that (*variable number of choices*)? – nik_m Mar 26 '17 at 18:22
  • From screenshot if you see there are choices. How I can access them from Save method? I got Ballot Text, Address but how about Choice text's and choice addresses? – Sandeep Thota Mar 26 '17 at 20:55
  • Because `choices` is an inline form inside the `ballot`, to access `choices` you should override [`save_formset`](https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_formset). – nik_m Mar 27 '17 at 07:10
  • Thanks again. def save_formset(self, request, form, formset, change): for f in formset: print('Voter address is: ', f['voter_address'] ) super().save_formset(request,form, formset, change) I get output as Voter address is: But how I can get value? "klncklas," is what I am looking for – Sandeep Thota Mar 27 '17 at 18:48
  • I am afraid that should be asked as a separate question, in order to be searchable by the community. However, take a look at [this](http://stackoverflow.com/questions/6988878/django-admin-how-to-save-inlines). – nik_m Mar 27 '17 at 18:57
  • I have added a new [question](http://stackoverflow.com/questions/43054736/django-extract-value-from-formset) can you please have a look and let me know the answer – Sandeep Thota Mar 27 '17 at 21:18