Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make any_field_blank optional #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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