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

remove get_by_natural_key implementation on UserProfileManager #2364

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Kakadus
Copy link
Collaborator

@Kakadus Kakadus commented Jan 6, 2025

leftover from #2249.

You can see an effect, if using the "natural key notation" [null]" for a UserProfile fk:

WITHOUT remove get_by_natural_key implementation on UserProfileManager, loading the fixture below results in

django.core.serializers.base.DeserializationError: Problem installing fixture '/home/jonas/development/evap/breakage.json': get() returned more than one UserProfile -- it returned 2!: (evaluation.evaluation:pk=356000) field_value was '[None]'

while WITH the commit, the error is (correctly)

django.core.serializers.base.DeserializationError: Problem installing fixture '/home/jonas/development/evap/breakage.json': ['“[None]” value must be an integer.']: (evaluation.evaluation:pk=356000) field_value was '[None]'

json fixture to load:

[{"model": "evaluation.evaluation","pk": 356000,"fields": {"state": 80,"course": 141,"name_de": "","name_en": "","weight": 1,"is_single_result": false,"is_rewarded": true,"is_midterm_evaluation": false,"can_publish_text_results": true,"_participant_count": 62,"_voter_count": 35,"vote_start_datetime": "2022-02-02T00:00:00","vote_end_date": "2022-02-12","allow_editors_to_edit": true,"wait_for_grade_upload_before_publishing": true,"participants": [[null]],"voters": []}}]

I cannot think of any test for this, but browsing the django code revealed no critical changes.

@Kakadus Kakadus force-pushed the profile-manager-no-natural-keys branch from 7b5a095 to 988355d Compare January 6, 2025 21:54
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I do want to note here that get_by_natural_keys is required by django.contrib.auth.handers.modwsgi, but we don't use it.

@Kakadus
Copy link
Collaborator Author

Kakadus commented Jan 13, 2025

Dö döööm. create_super_user tries to check if there is a get_by_natural_key method by checking if the email field is unique. Ours is unique but also null=True

@Kakadus
Copy link
Collaborator Author

Kakadus commented Jan 13, 2025

Here is the code in question:

    def _validate_username(self, username, verbose_field_name, database):
        """Validate username. If invalid, return a string error message."""
        if self.username_is_unique:
            try:
                self.UserModel._default_manager.db_manager(database).get_by_natural_key(
                    username
                )
            except self.UserModel.DoesNotExist:
                pass
            else: ...

which crashes when we do not have get_by_natural_key implemented. username_is_unique is true because of

    @cached_property
    def username_is_unique(self):
        if self.username_field.unique:
            return True
        return any(
            len(unique_constraint.fields) == 1
            and unique_constraint.fields[0] == self.username_field.name
            for unique_constraint in self.UserModel._meta.total_unique_constraints
        )

and we do have unique=True but also null=False.

https://github.com/django/django/blob/main/django/contrib/auth/management/commands/createsuperuser.py#L291-L309

@niklasmohrin and I concluded that this is a django bug, I will open another ticket later.

@Kakadus Kakadus marked this pull request as draft January 13, 2025 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants