# Django form debugging

Evaluating Django form-handling code to identify bugs, inefficiencies, and security issues.

|---|---|
| You can query a model directly and test the results returned when using different parameter values with the Cloud console, or by calling the Vertex AI API directly. - For an example of using the Vertex AI API, see [Quickstart using the Vertex AI API](https://docs.cloud.google.com/vertex-ai/generative-ai/docs/start/quickstarts/api-quickstart). - To view this sample in the Cloud console: [Go to Google Cloud console](https://console.cloud.google.com/vertex-ai/generative/language/prompt-examples/Django%20form%20debugging) <br /> ||
| #### Prompt Freeform Review the following Django form handling code designed for a college application and test submission process. The code manages the collection and validation of applicant information, including personal details, academic records, and standardized test scores. Identify any bugs, inefficiencies, security risks, or deviations from Django best practices and PEP 8 standards. from django import forms from django.core.exceptions import ValidationError from college.models import Application, AcademicRecord, TestScore from django.utils.translation import gettext_lazy as _ from django.db import transaction class CollegeApplicationForm(forms.ModelForm): class Meta: model = Application fields = ['first_name', 'last_name', 'email', 'phone_number', 'address', 'birthdate', 'intended_major'] gpa = forms.DecimalField(max_digits=4, decimal_places=2, min_value=0.0, max_value=4.0) sat_score = forms.IntegerField(min_value=400, max_value=1600, required=False) act_score = forms.IntegerField(min_value=1, max_value=36, required=False) transcript = forms.FileField(required=True) def clean(self): cleaned_data = super().clean() sat_score = cleaned_data.get('sat_score') act_score = cleaned_data.get('act_score') if not sat_score and not act_score: raise ValidationError(_('At least one test score (SAT or ACT) is required.')) if cleaned_data.get('emai') and Application.objects.filter(email=cleaned_data.get('email')).exists(): raise ValidationError(_('An application with this email already exists.')) return cleaned_data def save(self, commit=True): with transaction.atomic(): application = super().save(commit=False) academic_record = AcademicRecord( application=application, gpa=self.cleaned_data['gpa'], transcript=self.cleaned_data['transcript'] ) if commit: application.save() academic_record.save() if self.cleaned_data.get('sat_score'): TestScore.objects.create( application=application, test_type='SAT', score=self.cleaned_data['sat_score'] ) if self.cleaned_data.get('act_score'): TestScore.objects.create( application=application, test_type='ACT', score=self.cleaned_data['act_score'] ) return application #### Response Of course. Here is a detailed review of the provided Django form handling code, broken down by category. ### Summary of Findings The code attempts to handle a complex object creation process within a single form, which is a valid but advanced pattern. However, it contains several critical bugs that will cause it to fail or behave incorrectly, a significant data integrity risk, and several deviations from Django best practices and PEP 8 standards. *** ** * ** *** ### 1. Critical Bugs These are issues that will cause runtime errors or prevent the code from working as intended. #### Bug 1: Typo in `clean()` Method In the `clean()` method, the email uniqueness check has a typo. - **Code:** `if cleaned_data.get('emai') ...` - **Problem:** It checks for the key `'emai'` instead of `'email'`. Because `cleaned_data.get('emai')` will always return `None`, **this uniqueness check will never run**, allowing duplicate applications to be submitted. - **Fix:** Correct the typo from `emai` to `email`. #### Bug 2: Saving Related Objects When `commit=False` The `save()` method does not correctly respect the `commit=False` argument. - **Code:** The `TestScore.objects.create(...)` calls are outside the `if commit:` block. - **Problem:** If the form is saved with `form.save(commit=False)`, the `Application` and `AcademicRecord` instances are correctly held in memory without being saved to the database. However, the `TestScore` objects will be created and saved to the database immediately, violating the purpose of `commit=False` and leaving orphaned `TestScore` records if the parent `application` is never saved. - **Fix:** All database-writing operations must be moved inside the `if commit:` block. #### Bug 3: Saving a Related Model with an Unsaved Foreign Key The order of operations in the `save()` method is incorrect. - **Code:** `python application = super().save(commit=False) academic_record = AcademicRecord(application=application, ...) # ... if commit: application.save() academic_record.save()` - **Problem:** `academic_record` is instantiated with an `application` object that has not yet been saved to the database (it has no primary key). When `academic_record.save()` is called, it will fail with an `IntegrityError` because the foreign key to `Application` (`application_id`) will be `NULL`. - **Fix:** The parent `application` object must be saved *before* any child objects that reference it are saved. *** ** * ** *** ### 2. Security \& Data Integrity Risks #### Risk 1: Race Condition in Email Uniqueness Check Even with the typo fixed, the manual uniqueness check in the `clean()` method is vulnerable to a race condition. - **Problem:** Two users could submit a form with the same email at nearly the same time. Both forms could pass the `clean()` method's check (`Application.objects.filter(...).exists()`) before either is saved. The first to hit the database will save successfully, but the second will raise an `IntegrityError`, resulting in an unhandled server error (500) instead of a clean validation message to the user. - **Best Practice:** The canonical way to enforce uniqueness in Django is at the model level. The `email` field on the `Application` model should be defined with `unique=True`. `python # In college/models.py class Application(models.Model): email = models.EmailField(unique=True) # ... other fields` `ModelForm` validation will automatically handle this check correctly and without race conditions. The manual check in `clean()` should be removed. *** ** * ** *** ### 3. Inefficiencies \& Django Best Practices #### Issue 1: Form Structure and Field Definition The form defines fields (`gpa`, `sat_score`, etc.) that are not on the `Application` model. - **Observation:** This is a valid pattern for handling related models, but it's important to be explicit about it. The current structure works, but it can be confusing. The fields are correctly defined at the class level, as they are not part of the `Meta` model's fields. - **Best Practice:** The implementation is acceptable, but the `save()` method, which orchestrates the creation of these related models, must be flawless. The use of `transaction.atomic()` is an excellent practice here, but its benefits are negated by the bugs within the `save` method. #### Issue 2: Multiple Database Hits in `save()` The `save()` method creates `TestScore` objects with separate `TestScore.objects.create()` calls. - **Problem:** This results in two separate `INSERT` queries to the database. While not a major issue for only two objects, it's less efficient than creating them in a single query. - **Best Practice:** For creating multiple objects of the same type, use `bulk_create()`. This sends a single, more efficient query to the database. *** ** * ** *** ### 4. PEP 8 \& Code Style - **Missing Docstrings:** The form class `CollegeApplicationForm` lacks a docstring explaining its purpose. - **Vertical Spacing:** There should be a blank line between the `Meta` class and the field definitions for better readability. *** ** * ** *** ### Corrected and Refactored Code Here is the revised code that addresses all the identified issues. from django import forms from django.core.exceptions import ValidationError from college.models import Application, AcademicRecord, TestScore from django.utils.translation import gettext_lazy as _ from django.db import transaction class CollegeApplicationForm(forms.ModelForm): """ A form for handling college applications, including academic records and standardized test scores. This form creates an Application instance and its related AcademicRecord and TestScore objects in a single atomic transaction. """ # Fields for related models (AcademicRecord and TestScore) gpa = forms.DecimalField(max_digits=3, decimal_places=2, min_value=0.0, max_value=4.0) transcript = forms.FileField(required=True) sat_score = forms.IntegerField(min_value=400, max_value=1600, required=False) act_score = forms.IntegerField(min_value=1, max_value=36, required=False) class Meta: model = Application fields = [ 'first_name', 'last_name', 'email', 'phone_number', 'address', 'birthdate', 'intended_major' ] # Note: It's assumed that Application.email has `unique=True` # which makes the manual uniqueness check in clean() redundant and unsafe. def clean(self): """ Ensure that at least one standardized test score is provided. """ cleaned_data = super().clean() sat_score = cleaned_data.get('sat_score') act_score = cleaned_data.get('act_score') if not sat_score and not act_score: # This attaches the error to a specific field for better UX self.add_error('sat_score', _('At least one test score (SAT or ACT) is required.')) # Alternatively, for a non-field error at the top of the form: # raise ValidationError(_('At least one test score (SAT or ACT) is required.')) return cleaned_data def save(self, commit=True): """ Saves the Application and its related models. If commit=True, this method saves the application, academic record, and any provided test scores to the database within a single transaction. """ # Create the Application instance in memory but don't save it yet. # This allows us to work with the object before committing to the DB. application = super().save(commit=False) if commit: # Use a transaction to ensure all or no objects are created. with transaction.atomic(): # 1. Save the parent object first to get a primary key. application.save() # 2. Create and save the related AcademicRecord. AcademicRecord.objects.create( application=application, gpa=self.cleaned_data['gpa'], transcript=self.cleaned_data['transcript'] ) # 3. Prepare TestScore objects for bulk creation. test_scores_to_create = [] if self.cleaned_data.get('sat_score'): test_scores_to_create.append( TestScore( application=application, test_type='SAT', score=self.cleaned_data['sat_score'] ) ) if self.cleaned_data.get('act_score'): test_scores_to_create.append( TestScore( application=application, test_type='ACT', score=self.cleaned_data['act_score'] ) ) if test_scores_to_create: TestScore.objects.bulk_create(test_scores_to_create) return application | |---|---| | **Model:** | gemini-2.5-pro | | **Temperature:** | 0.2 | | **Max output tokens:** | 8192 | | **TopK:** | 40.0 | | **TopP:** | 0.95 | |