Problem when overriding '__init__' / 'save' methods (models.model)
|Reported by:||Eric||Owned by:||nobody|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
In this sample model :
import logging class Test(models.Model): title = models.CharField(max_length = 50) def __init__(self, *args, **kwargs): super(Test, self).__init__(*args, **kwargs) self.title = 'a_' + self.title logging.debug ('in_init : %s' % self.title) def save(self): logging.debug ('in_save : %s' % self.title) self.title = self.title[2:] logging.debug ('in_save2 : %s' % self.title) return super(Test, self).save() def __unicode__(self): return self.title class Admin: pass
The save fonction was called twice and so, the field 'title' does not contains the value without the 2 first characters 'a_' where add init.
Try this :
Add a 'test' row with title : 'This is a test'; saved; the list show 'a_This is a test', this is right at this moment. Edit it and without changing anything, hit 'Save and edit' button. 'a_' is append at the beginning of the title each time row was saved. Same problem with signals 'pre_save'.
If I try with she shell 'manager.py shell', no problems.
Change History (2)
comment:1 Changed 6 years ago by Eric
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Summary changed from Save in models was called twice to Problem when overriding '__init__' / 'save' methods (models.model)