What's the difference between these two ways to override the save() method in a Django ModelForm?

ぃ、小莉子 提交于 2019-12-23 00:26:53

问题


I've come across two methods of doing this. The accepted answer here suggests:

def save(self, *args, **kwargs):
    instance = super(ModelClass, self).save(commit=False)
    instance.my_stuff = manual_value
    instance.save()

But the following, found here, seems more elegant:

def save(self, *args, **kwargs):
    self.my_stuff = manual_value
    super(ModelClass, self).save(*args, **kwargs)

Is there any reason to choose one over the other, other than the latter being one less line, such as a reason for running the parent save() first?


回答1:


The two examples are doing different things. The first is saving the model form's save method, the second is overriding the model's save method.

It only makes sense to override the model's method if you want the value to be set every time the model is saved. If updating the field is related to the form, then overriding the form's save method is the correct thing to do.

In the model form's save method, you have to call save(commit=False) first to get the instance. I wouldn't worry about it being inelegant, it's a very common pattern in Django, and is documented here.




回答2:


First one will create instance of model, without saving it, then you can add some value (that is required or not) and manually trigger save on that instance.

Second one will save some field on ModelForm (not on actual instance of your model) and then create + save instance of your model.

If in second one you're just setting value on form field that corresponds to model field edited in first example, that will almost work in same way.

Why almost? On second example you have to have that form field inside your form class, on first example you don't. If that field is required, and have been left empty, second example won't validate.

That being said, first example can add or change fields in your model that haven't appeared on form, second example can only modify fields that have been specified inside form class.



来源:https://stackoverflow.com/questions/32497711/whats-the-difference-between-these-two-ways-to-override-the-save-method-in-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!