django calls validate_unique twice - workaround? -


i understand validate_unique called when doing full_clean, in turn called when calling modelform.save() - means validate_unique won't automatically called when doing model_instance.save()

eg. see answer: https://stackoverflow.com/a/14472335/996792

i do want call validate_unique when calling model_instance.save i've overridden model's save function follows:

def save(self, *args, **kwargs):     self.validate_unique()     super(mymodel, self).save(*args, **kwargs) 

however, produces following quirk: when saving modelform (eg. in admin), validate_unique called twice! presumably once modelform.save() , once model.save().

is there anyway round inefficiency?

i detest unnecessary cruft , sort of thing bothers me.

this possible workaround:

clean( method called when request comes modelform, set flag when method called:

def clean( self ):     self.clean_called = true   #<---- flag.     #other model checks 

overwrite save( order call validate_unique if flag not set. don't forget remove flag.

def save(self, *args, **kwargs):     flag_is_set = hasattr( self, 'clean_called' ) , self.clean_called     if not flag_is_set:         self.validate_unique()     super(mymodel, self).save(*args, **kwargs)     self.clean_called = false 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -