confirmation_fields and confirm_change works as expected
parent
47b1d6beee
commit
b8cfdb591b
|
|
@ -10,7 +10,10 @@ from django.utils.translation import gettext as _
|
|||
class AdminConfirmMixin(object):
|
||||
"""Generic AdminConfirm Mixin"""
|
||||
|
||||
# Should we ask for confirmation for changes?
|
||||
confirm_change = None
|
||||
|
||||
# if confirm_change, which fields should we confirm for?
|
||||
confirmation_fields = None
|
||||
|
||||
# Custom templates (designed to be over-ridden in subclasses)
|
||||
|
|
@ -20,7 +23,7 @@ class AdminConfirmMixin(object):
|
|||
"""
|
||||
Hook for specifying confirmation fields
|
||||
"""
|
||||
if self.confirmation_fields:
|
||||
if self.confirmation_fields is not None:
|
||||
return self.confirmation_fields
|
||||
|
||||
return flatten_fieldsets(self.get_fieldsets(request, obj))
|
||||
|
|
@ -99,7 +102,7 @@ class AdminConfirmMixin(object):
|
|||
for name, field in form.fields.items():
|
||||
initial_value = obj.__getattribute__(name)
|
||||
new_value = new_object.__getattribute__(name)
|
||||
if field.has_changed(initial_value, new_value):
|
||||
if field.has_changed(initial_value, new_value) and initial_value != new_value:
|
||||
changed_data[name] = [initial_value, new_value]
|
||||
|
||||
if not bool(set(self.get_confirmation_fields(request, obj)) & set(changed_data.keys())):
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
{% load i18n admin_urls %}
|
||||
|
||||
{% block submit-row %}
|
||||
<input hidden name="_confirm_change" value="{{ confirm_change }}" />
|
||||
{% if confirm_change %}
|
||||
<input hidden name="_confirm_change" value="{{ confirm_change }}" />
|
||||
{% endif %}
|
||||
{{ block.super }}
|
||||
{% endblock %}
|
||||
|
|
|
|||
|
|
@ -7,18 +7,17 @@ from .models import Item, Inventory, Shop
|
|||
|
||||
class ItemAdmin(AdminConfirmMixin, admin.ModelAdmin):
|
||||
list_display = ('name', 'price', 'currency')
|
||||
require_change_confirmation = True
|
||||
confirm_change = True
|
||||
|
||||
|
||||
class InventoryAdmin(AdminConfirmMixin, admin.ModelAdmin):
|
||||
list_display = ('shop', 'item', 'quantity')
|
||||
requires_change_confirmation = {
|
||||
'fields': ['shop']
|
||||
}
|
||||
confirm_change = True
|
||||
confirmation_fields = ['shop']
|
||||
|
||||
|
||||
class ShopAdmin(admin.ModelAdmin):
|
||||
pass
|
||||
class ShopAdmin(AdminConfirmMixin, admin.ModelAdmin):
|
||||
confirmation_fields = ['name']
|
||||
|
||||
|
||||
admin.site.register(Item, ItemAdmin)
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ class Inventory(models.Model):
|
|||
class Meta:
|
||||
unique_together = ['shop', 'item']
|
||||
ordering = ['shop', 'item__name']
|
||||
verbose_name_plural = 'Inventory'
|
||||
|
||||
shop = models.ForeignKey(to=Shop, on_delete=models.CASCADE, related_name='inventory')
|
||||
item = models.ForeignKey(to=Item, on_delete=models.CASCADE)
|
||||
|
|
|
|||
Loading…
Reference in New Issue