-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
refactor(socialaccount): Avoid database lookup at is_existing
#3808
Conversation
# Conflicts: # ChangeLog.rst # allauth/socialaccount/providers/tiktok/provider.py
You did not copy the full code (either that, or, you are using an outdated version?). The full code is:
So, even if |
I do have it. However, the current code will then perform multiple database calls (I logged 2) where we are actually 100% certain that the user does not exist at this point because the object was just created. The change I'm proposing handles the id+default scenario. It seems to me the intent was to create a User object without a pk set, otherwise why would the line |
The intention of this PR was not fully clear to me:
Suggests that the code is wrong, where it is actually correct. If we are talking about # of DB queries, that is a different matter. Though, perhaps it is better then to check |
Thanks for the pointer. I didn't look into what this does so I'll check it out and update the PR. |
I suspect -- haven't tested it myself -- that only doing |
I really don't think it is necessary to keep track of this state ourselves, as Django already does that for you:
|
is_existing
Hmm, while this works in most cases, I am afraid it can subtly break. For example:
Now, while that serialization can be fixed on this end, if projects are doing customized things things go wrong. All in all, I don't think the costs outweigh the benefits of this one query. |
In that case, my initial suggestion is fine? e.g. pk=None |
If you have a Django In any case, seems a bit tricky to me. Safer would be:
|
I tried cached_property, a bunch of tests failed because of it. But no, I've tested that behavior. Setting it to None is fine, the value is generated when you save. I'll find the line. |
|
Interesting, the behavior is different for non PKs. Take this model:
|
That may be because of tests doing non-standard things. Will have a look at a later time. I am still a bit worried about |
https://github.com/django/django/blob/main/django/db/models/fields/__init__.py#L742 |
Adjusting |
In my opinion, when you override the default it simply becomes your responsibility to keep any of the original intent (if you want it). Adding a bunch of boilerplate code to achieve the same result reached by the default doesn't make sense to me. |
Simple PR that makes sure a newly created user model does not have a pk assigned, which is otherwise automatically assigned if the field is defined on the model and has a default value set. For example, this model would currently have a pk assigned:
Thus, this code does not behave as expected: