有条件地覆盖模型Django admin成功保存的消息

时间:2023-02-10 19:23:16

I am overriding the save method on a Django model. There are some cases where I do not save the model. In these cases, I can't seem to be able to figure out how to conditionally override the "The citation 1111 was added successfully." message that is shown after returning back to the admin list interface (as opposed to the entry form interface).

我在Django模型上覆盖了save方法。在某些情况下,我不保存模型。在这些情况下,我似乎无法弄清楚如何有条件地覆盖“引用1111已成功添加”。返回管理列表界面后显示的消息(与输入表单界面相反)。

1 个解决方案

#1


1  

I don't think you can override that just by overriding save Django's admin interface uses model forms and the messages framework.

我不认为你可以通过重写来覆盖它。保存Django的管理界面使用模型表单和消息框架。

I think something like this happens, it's more complicated than this but more or less:

我觉得这样的事情发生了,它比这更复杂但是或多或少:

models.py

class MyModel(models.Model):
    foo = models.CharField(...)
    bar = models.CharField(...)

    def save(self, *args, **kwargs):
        if self.foo == self.bar:  # We only save if foo == bar
            super(MyModel, self).save(*args, **kwargs)

forms.py (Django admin uses model forms, so this is an example)

class MyModelForm(ModelForm):
    class Meta:
        model = MyModel

views.py

def save(request):
    if request.method == 'POST':
        form = MyModelForm(request.POST)
        if form.is_valid():
            form.save()
            messages.success(request, 'MyModel was saved!.')

Now regardless of what form.save() actually did the message is still sent out anyway, we have no way of knowing if you saved or not in your overridden save method and this is probably whats happening in the django admin system.

现在无论form.save()实际上是什么消息仍然被发送出去,我们无法知道你是否保存了你的重写保存方法,这可能是django管理系统中发生的事情。

An alternative would be to create a custom model form for the admin to use for this model and define a custom validation method, so the form doesn't validates unless foo == bar. Or you could override the save method on the form, you'll need to have a look around the django admin code, it probably is possible, just a bit trixy :p

另一种方法是为管理员创建一个自定义模型表单以用于此模型并定义自定义验证方法,因此除非foo == bar,否则表单不会验证。或者你可以覆盖表单上的save方法,你需要看看django管理代码,它可能是有可能的,只是有点trixy:p

#1


1  

I don't think you can override that just by overriding save Django's admin interface uses model forms and the messages framework.

我不认为你可以通过重写来覆盖它。保存Django的管理界面使用模型表单和消息框架。

I think something like this happens, it's more complicated than this but more or less:

我觉得这样的事情发生了,它比这更复杂但是或多或少:

models.py

class MyModel(models.Model):
    foo = models.CharField(...)
    bar = models.CharField(...)

    def save(self, *args, **kwargs):
        if self.foo == self.bar:  # We only save if foo == bar
            super(MyModel, self).save(*args, **kwargs)

forms.py (Django admin uses model forms, so this is an example)

class MyModelForm(ModelForm):
    class Meta:
        model = MyModel

views.py

def save(request):
    if request.method == 'POST':
        form = MyModelForm(request.POST)
        if form.is_valid():
            form.save()
            messages.success(request, 'MyModel was saved!.')

Now regardless of what form.save() actually did the message is still sent out anyway, we have no way of knowing if you saved or not in your overridden save method and this is probably whats happening in the django admin system.

现在无论form.save()实际上是什么消息仍然被发送出去,我们无法知道你是否保存了你的重写保存方法,这可能是django管理系统中发生的事情。

An alternative would be to create a custom model form for the admin to use for this model and define a custom validation method, so the form doesn't validates unless foo == bar. Or you could override the save method on the form, you'll need to have a look around the django admin code, it probably is possible, just a bit trixy :p

另一种方法是为管理员创建一个自定义模型表单以用于此模型并定义自定义验证方法,因此除非foo == bar,否则表单不会验证。或者你可以覆盖表单上的save方法,你需要看看django管理代码,它可能是有可能的,只是有点trixy:p