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

fix: null number field #29

Open
wants to merge 5 commits 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
10 changes: 7 additions & 3 deletions src/django_fields/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ def _get_padding(self, value):
mod = (len(value) + 2) % self.cipher.block_size
return self.cipher.block_size - mod + 2


def to_python(self, value):
if self._is_encrypted(value):
return force_unicode(
Expand All @@ -69,6 +68,9 @@ def to_python(self, value):
return value

def get_db_prep_value(self, value, connection=None, prepared=False):
if value is None:
return None

value = smart_str(value)

if value is not None and not self._is_encrypted(value):
Expand Down Expand Up @@ -192,15 +194,17 @@ def get_internal_type(self):

def to_python(self, value):
# value is either an int or a string of an integer
if isinstance(value, self.number_type):
if isinstance(value, self.number_type) or value is None:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to add import types to the top, and use if isinstance(value, (self.number_type, types.NoneType)): instead of if isinstance(value, self.number_type) or value is None:.

number = value
else:
number_text = super(BaseEncryptedNumberField, self).to_python(value)
number = self.number_type(number_text)
return number

# def get_prep_value(self, value):
def get_db_prep_value(self, value, connection=None, prepared=False):
if value is None:
return None

number_text = self.format_string % value
return super(BaseEncryptedNumberField, self).get_db_prep_value(
number_text,
Expand Down