-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
Fixed #36482 -- Rendered PK with value_to_string() in admin. #19600
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
base: main
Are you sure you want to change the base?
Conversation
@property | ||
def pk_str(self): | ||
return self._meta.pk.value_to_string(self) |
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.
I know adding this may be contentious but we could use it everywhere else value_to_string()
is used - in serialisation & sessions.
django/db/models/fields/json.py
Outdated
def to_python(self, value): | ||
return json.loads(value) | ||
|
||
def value_to_string(self, obj): | ||
return self.value_from_object(obj) | ||
return json.dumps(self.value_from_object(obj)) |
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 is a separate issue(?) that I noticed while trying to find a suitable way to setup a unit test: JSONField wasn't doing any proper json conversion to/from string values. Perhaps nobody cares 🤷♂️
…e_to_string()... - JSONField had no to_python() so the base implementation was used. - value_to_string() was simply deferring to value_from_object() without json processing.
Trac ticket number
ticket-36482
Branch description
This PR demonstrates how to render a PK "correctly", using
field.value_to_string(obj)
, so thatget_object()
will correctly retrieve value when it usesto_python()
to deserialise the PK back to a Python type.You can test this with the following example:
Note that there are places that still need updating, like here where the pk is
rendered as a string directlypassed toquote()
inside the template where we cannot usefield.value_to_string(obj)
:django/django/contrib/admin/templates/admin/change_form_object_tools.html
Line 4 in a388287
Checklist
main
branch.