Skip to content

Commit

Permalink
allow keeping default values for RandomCharField (#1682)
Browse files Browse the repository at this point in the history
  • Loading branch information
ladmerc committed Jun 11, 2022
1 parent cf13bee commit 5be2771
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion django_extensions/db/fields/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ class RandomCharField(UniqueFieldMixin, CharField):
include_punctuation
If set to True, include punctuation characters (default: False)
keep_default
If set to True, keeps the default initialization value (default: False)
"""

def __init__(self, *args, **kwargs):
Expand All @@ -341,6 +344,7 @@ def __init__(self, *args, **kwargs):
self.include_alpha = kwargs.pop('include_alpha', True)
self.check_is_bool('include_alpha')
self.include_punctuation = kwargs.pop('include_punctuation', False)
self.keep_default = kwargs.pop('keep_default', False)
self.check_is_bool('include_punctuation')
self.max_unique_query_attempts = kwargs.pop('max_unique_query_attempts', MAX_UNIQUE_QUERY_ATTEMPTS)

Expand All @@ -362,7 +366,7 @@ def in_unique_together(self, model_instance):
return False

def pre_save(self, model_instance, add):
if not add and getattr(model_instance, self.attname) != '':
if (not add or self.keep_default) and getattr(model_instance, self.attname) != '':
return getattr(model_instance, self.attname)

population = ''
Expand Down

0 comments on commit 5be2771

Please sign in to comment.