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

Use attname to get pk value #650

Merged
merged 4 commits into from
Jun 11, 2024

Conversation

evanandrews-xrd
Copy link
Contributor

If a model's primary key is another model, the current implementation of LogEntryManager._get_pk_value will needlessly pull in the entire object only to extract its primary key and discard it. This change gets the primary key directly from the first object.

The attname field isn't particularly well documented in Django, but it is briefly mentioned here.

On the base Field class, it is the same as name:

https://github.com/django/django/blob/adae619426b6f50046b3daaa744db52989c9d6db/django/db/models/fields/__init__.py#L973-L974

Whereas for ForeignKey, it is {name}_id:

https://github.com/django/django/blob/adae619426b6f50046b3daaa744db52989c9d6db/django/db/models/fields/related.py#L1117-L1118

Happy to remove the assert if you'd prefer not to use it.

Copy link

codecov bot commented Jun 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.29%. Comparing base (5289482) to head (e848b75).
Report is 15 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #650      +/-   ##
==========================================
+ Coverage   95.21%   95.29%   +0.07%     
==========================================
  Files          31       31              
  Lines        1025     1041      +16     
==========================================
+ Hits          976      992      +16     
  Misses         49       49              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@hramezani
Copy link
Member

Thanks @evanandrews-xrd for this PR.

It would be great to add a test for the change. it can be a small test only for LogEntryManager._get_pk_value

@evanandrews-xrd
Copy link
Contributor Author

I've added some tests, although looking at this further it might not be necessary to have LogEntryManager._get_pk_value at all. Django provides the Model.pk property which gives you the value of the primary key:

https://docs.djangoproject.com/en/5.0/ref/models/instances/#the-pk-property

https://github.com/django/django/blob/adae619426b6f50046b3daaa744db52989c9d6db/django/db/models/base.py#L653-L663

The only difference now between LogEntryManager._get_pk_value(instance) and instance.pk is that the former will return None if the primary key attribute is (somehow) missing, while the latter will raise AttributeError.

If you'd prefer, we can probably just delete the method and use instance.pk where it's called.

@hramezani
Copy link
Member

Thanks @evanandrews-xrd for the update and providing more information.

The only difference now between LogEntryManager._get_pk_value(instance) and instance.pk is that the former will return None if the primary key attribute is (somehow) missing, while the latter will raise AttributeError.

If you'd prefer, we can probably just delete the method and use instance.pk where it's called.

Then we need to catch AttributeError everywhere we use _get_pk_value to prevent breaking change.
I would prefer to keep the method

hramezani
hramezani previously approved these changes Jun 4, 2024
@hramezani
Copy link
Member

Let me know if you are done with this PR. then we can merge it

@evanandrews-xrd
Copy link
Contributor Author

I've just added a couple of extra comments to include what I touched on above. Happy for you to merge when ready. Thanks!

@hramezani hramezani merged commit 2c0bd0f into jazzband:master Jun 11, 2024
8 checks passed
@hramezani
Copy link
Member

Thanks @evanandrews-xrd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants