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
Fixed #29869 -- Made UUIDField.to_python() convert int values. #10557
Conversation
django/db/models/fields/__init__.py
Outdated
@@ -2319,6 +2319,8 @@ def get_db_prep_value(self, value, connection, prepared=False): | |||
|
|||
def to_python(self, value): | |||
if value is not None and not isinstance(value, uuid.UUID): | |||
if isinstance(value, int): | |||
return uuid.UUID(int=value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be done in the ValueError
try
/except
below as this will fail for integer larger than 128 bits. A test should be added for this case as well.
> uuid.UUID(int=(2 ** 128) - 1)
UUID('ffffffff-ffff-ffff-ffff-ffffffffffff')
> uuid.UUID(int=(2 ** 128))
ValueError: int is out of range (need a 128-bit value)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, yes! We should check that.
I've modified the patch to keep a check on these things. Can you have a look now, please?
22834a6
to
1f3fb8f
Compare
django/db/models/fields/__init__.py
Outdated
self.error_messages['invalid'], | ||
code='invalid', | ||
params={'value': value}, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should find a way to reduce the duplication between the two blocks. What about
form = 'int' if isinstance(value, int) else 'hex'
try:
return uuid.UUID(**{form: value})
except (AttributeError, ValueError):
raise exceptions.ValidationError(
self.error_messages['invalid'],
code='invalid',
params={'value': value},
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this actually looks good to me!
I've made these change! Thanks a lot for you suggestion :)
1f3fb8f
to
01e3818
Compare
01e3818
to
deb9caa
Compare
deb9caa
to
83c7096
Compare
Thanks for reviewing and merging |
https://code.djangoproject.com/ticket/29869