Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 16, 2025

The test_form_creates_new_repositories test was failing because the form's repositories field had an empty queryset when creating new hackathons, preventing valid repository selections from passing validation.

Root Cause

In HackathonForm.__init__, the repositories queryset was unconditionally set to Repo.objects.none() for new instances:

if self.instance.pk and self.instance.organization:
    self.fields["repositories"].queryset = Repo.objects.filter(...)
else:
    self.fields["repositories"].queryset = Repo.objects.none()  # ❌ Too restrictive

Django validates form choices against the field's queryset before calling clean_repositories(), so the form would fail validation with "Select a valid choice. X is not one of the available choices" even when selecting valid repositories from the chosen organization.

Changes

Updated __init__ to populate the repositories queryset from the organization in the form data:

else:
    organization_id = None
    if "data" in kwargs and kwargs["data"]:
        organization_id = kwargs["data"].get("organization")
    
    if organization_id:
        self.fields["repositories"].queryset = Repo.objects.filter(organization_id=organization_id)
    else:
        self.fields["repositories"].queryset = Repo.objects.none()

This allows existing repositories from the selected organization to be validated and selected when creating new hackathons.

Original prompt

please fix ======================================================================
FAIL: test_form_creates_new_repositories (website.tests.test_hackathon_form.HackathonFormTestCase.test_form_creates_new_repositories)
Test that form creates new repositories when saved.

Traceback (most recent call last):
File "/home/runner/work/BLT/BLT/website/tests/test_hackathon_form.py", line 78, in test_form_creates_new_repositories
self.assertTrue(form.is_valid())
AssertionError: False is not true


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 16, 2025

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions bot added the unresolved-conversations: 0 PR has 0 unresolved conversations label Nov 16, 2025
@github-actions github-actions bot added the files-changed: 2 PR changes 2 files label Nov 16, 2025
Copilot AI changed the title [WIP] Fix form validation issue for new repository creation Fix HackathonForm repositories queryset validation for new hackathons Nov 16, 2025
Copilot AI requested a review from DonnieBLT November 16, 2025 22:08
@github-actions github-actions bot added files-changed: 1 pre-commit: passed Pre-commit checks passed and removed files-changed: 2 PR changes 2 files labels Nov 16, 2025
@DonnieBLT DonnieBLT marked this pull request as ready for review November 16, 2025 22:23
@DonnieBLT DonnieBLT merged commit 2f9fe41 into main Nov 16, 2025
21 checks passed
@DonnieBLT DonnieBLT deleted the copilot/fix-form-validation-issue branch November 16, 2025 22:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

files-changed: 1 pre-commit: passed Pre-commit checks passed unresolved-conversations: 0 PR has 0 unresolved conversations

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants