2009年11月18日水曜日

saveをオーバーライドする時はforce_insert、force_updateを渡す

Djangoでdjango.db.Modelsのsaveメソッドをオーバーライドする際


def save(self):


とすると、create()とget_or_create()で以下のようなエラーを吐いた。


TypeError: save() got an unexpected keyword argument 'force_insert'


saveにforce_insert,force_updateを渡すようにしないといけないらしい。
正解はこう


def save(self, force_insert=False, force_update=False):


http://code.djangoproject.com/wiki/BackwardsIncompatibleChanges


create() and get_or_create() will never update existing objects ¶

In [8670] a change was made to both create() and get_or_create() that affects people in two ways:

Firstly, if you have a custom save() method on your model and it is going to be called using create() or get_or_create(), you should make sure you accept the force_insert parameter (best to accept force_update as well, just like django.db.models.base.Model.save()). You don't have to do anything with these parameters, just pass them through to the Model.save() method.

1 件のコメント:

  1. You can discover single and multi-hand versions of the sport, as well as|in addition to} many different variants. There are additionally a few of} video games may be} a little more unusual, corresponding to Blackjack Switch and Blackjack Double Exposure, which convey some exciting twists to the principles. It is inconceivable to play in a region the place a on line casino is illegal the law|is unlawful}. Therefore, you must to} change your region of residence before 점보카지노 starting gambling actions on the official website of the gambling institution. Otherwise, you will get get} problems because of the laws of your country, and your account can be blocked at any second. We recommend you to learn carefully the principles of enjoying in} on Fairspin and keep in mind all of the authorized subtleties to start out|to begin} your gambling exercise safely.

    返信削除