Created
August 4, 2015 22:39
-
-
Save sesh/9c053497d760b2e3943f to your computer and use it in GitHub Desktop.
Django Form Defaults
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.db import models | |
| from django import forms | |
| from django.test import TestCase, RequestFactory | |
| from .models import BooleanTestModel | |
| """ | |
| models.py | |
| class BooleanTestModel(models.Model): | |
| name = models.CharField(max_length=200, default='Brenton') | |
| active = models.BooleanField(default=True) | |
| """ | |
| class BooleanTestForm(forms.ModelForm): | |
| class Meta: | |
| model = BooleanTestModel | |
| fields = ['name', 'active',] | |
| class FormBooleanTest(TestCase): | |
| def test_boolean_value(self): | |
| factory = RequestFactory() | |
| request = factory.post('/', {'name': ''}) | |
| form = BooleanTestForm(request.POST) | |
| self.assertTrue(form.is_valid()) | |
| instance = form.save() | |
| # `name` will use the default value from the model | |
| self.assertEqual('Brenton', instance.name) | |
| # `active` will always default to False | |
| self.assertFalse(instance.active) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment