python - Alternatives to creating and iterating through a list for "bad" values each time clean is called in django? -
python - Alternatives to creating and iterating through a list for "bad" values each time clean is called in django? -
let's have grouping of words don't want allow users include in titles going submitting. alternatives on how store values besides hardcoding list clean function?
i thought creating new model contain of these words aren't allowed not sure whether or not querying database each time clean called function slower/faster or more/less secure creating separate list names. think more readable if list long though.
i suggest create abstract model , utilize clean method there , later inherit models that:
from django.core.exceptions import validationerror django.db import models bad_words = ['abc', 'def'] class titlemixin(models.model): title = models.charfield(max_length=256) class meta: abstract = true def clean(self, *args, **kwargs): super(titlemixin, self).clean(*args, **kwargs) if any(w in self.title w in bad_words): raise validationerror('bad title') now utilize above abstract model create new models , have same functionality checking bad words in title:
class mynewmodel(titlemixin): pass python sql database django web
Comments
Post a Comment