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

Deleting form fields and Initializing querysets order #1653

Open
ilyadesign opened this issue Apr 24, 2024 · 1 comment
Open

Deleting form fields and Initializing querysets order #1653

ilyadesign opened this issue Apr 24, 2024 · 1 comment

Comments

@ilyadesign
Copy link

class StudentAchievementFilter(django_filters.FilterSet):
    def __init__(self, data=None, queryset=None, user=None, *args, **kwargs):
        super().__init__(data=data, queryset=queryset, *args, **kwargs)
        if user.get_group == 'teacher':
            del self.form.fields['teacher']
            self.filters['student'].queryset = user.teacher.get_students
            self.filters['team'].queryset = user.teacher.get_active_teams

The issue arises when del self.form.fields['teacher'] is placed before updating the querysets for 'student' and 'team'. In this scenario, it seems that the changes to the querysets do not get properly initialized.

Placing the deletion of the 'teacher' field before updating the querysets might interfere with the initialization process, possibly due to the internal mechanics of how Django handles form fields and queryset modifications. As a result, the querysets for 'student' and 'team' are not initialized.

@carltongibson
Copy link
Owner

Yes, don't access the form until after the filters are configured.

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

No branches or pull requests

2 participants