Python 从Django中的模型创建确认消息

Python 从Django中的模型创建确认消息,python,django,django-models,django-views,django-admin,Python,Django,Django Models,Django Views,Django Admin,我在Django有一个应用程序,它有多个模型。我有一个特别的模型如下: 型号.py class MyModel(models.Model): model_id= models.AutoField(primary_key=True) model_date = models.DateTimeField(verbose_name="Label 1") model_counter = models.IntegerField(blank=True, null=True) class

我在Django有一个应用程序,它有多个模型。我有一个特别的模型如下:

型号.py

class MyModel(models.Model):
model_id= models.AutoField(primary_key=True)
model_date = models.DateTimeField(verbose_name="Label 1")
model_counter = models.IntegerField(blank=True, null=True)
class MyModelAdmin(admin.ModelAdmin):
list_display = ('model_id', 'model_date ', 'model_counter ')
list_filter = (
    ('model_date', DropdownFilter)
)    
def has_delete_permission(self, request, obj=None):
    return False
def get_actions(self, request):
    actions = super().get_actions(request)
    if 'delete_selected' in actions:
        del actions['delete_selected']
    return actions
readonly_fields = ['model_counter ']
admin.site.register(MyModel, MyModelAdmin)
admin.py

class MyModel(models.Model):
model_id= models.AutoField(primary_key=True)
model_date = models.DateTimeField(verbose_name="Label 1")
model_counter = models.IntegerField(blank=True, null=True)
class MyModelAdmin(admin.ModelAdmin):
list_display = ('model_id', 'model_date ', 'model_counter ')
list_filter = (
    ('model_date', DropdownFilter)
)    
def has_delete_permission(self, request, obj=None):
    return False
def get_actions(self, request):
    actions = super().get_actions(request)
    if 'delete_selected' in actions:
        del actions['delete_selected']
    return actions
readonly_fields = ['model_counter ']
admin.site.register(MyModel, MyModelAdmin)
我需要的是,用户确认他想要保存模型,即使日期大于今天,在这样做时,用户可以创建一个可能对其他用户错误的模型,但有时日期大于今天是正确的

我不能使用自定义HTML表单,只能使用默认表单

用户必须以某种方式与页面进行交互,并直接确认他知道正在保存的新模型可能会对其他用户造成危险。用户必须能够中止保存或修改日期。 因此,我尝试使用模型的另一个字段来存储计数器:

def clean(self):
    condition = False
    if self.model_counter is None:
        condition = True
    else:
        condition = self.model_counter == 1

    if condition :
        self.model_counter = 1
        raise ValidationError("Attention, the date inserted is after the current date, click the SAVE button another time to proceed with the saving")
作为计数器,我使用同一型号的另一个字段。我无法使计数器的更新正常工作。据我所知,验证的生命周期阻止我以正确的方式更改它保存的代码所在的模型的距离状态,因此忽略了对字段模型的更新。 有什么方法可以实现我的目标?我使用model字段来存储counter的值,因为我无法以其他方式进行存储。我不管柜台在哪里。我也不想使用Django的消息系统或弹出窗口。我只需要在某些条件下强制用户进行交互,强制用户继续或中止保存

编辑
为了更加清晰,我还在admin.py中添加了代码。我只修改了模型和admin,调用命令:python3 manage.py inspectdb>models.py,生成了所有代码。这是我们公司的标准程序。因此,我无法向Django生成的网页添加(或不知道如何添加)代码。

我认为您最好在这里使用一些JavaScript。将
单击
事件添加到submit(提交)按钮,模式/dialag请求用户确认。如果他们说是,那么你可以提交表格

例如,使用dialog(如果需要,可以将其设置为模态):

HTML


Django以友好的方式创建此网页,如果我了解此过程,那么我可以修改此网页的创建吗?