Skip to content

Commit

Permalink
make any_field_blank optional
Browse files Browse the repository at this point in the history
  • Loading branch information
lampslave committed Apr 15, 2019
1 parent 75eb4a1 commit 261d25b
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions django_any/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,23 @@
@any_field.decorator
def any_field_blank(function):
"""
Sometimes return empty value if field could be blank
Sometimes return empty value if field could be blank and
`settings.ALWAYS_FILL_BLANK_FIELDS` is `False` (default)
"""
def wrapper(field, **kwargs):
# any_model(Entry, pub_date__isnull=True)
if kwargs.get('isnull', False):
return None

if field.blank and random.random() < 0.1:
if field.null:
return None
else:
try:
return field.to_python('')
except ValidationError as e: # bool, int, etc.
pass
if not getattr(settings, 'ALWAYS_FILL_BLANK_FIELDS', False):
if field.blank and random.random() < 0.1:
if field.null:
return None
else:
try:
return field.to_python('')
except ValidationError as e: # bool, int, etc.
pass

return function(field, **kwargs)
return wrapper
Expand Down

0 comments on commit 261d25b

Please sign in to comment.