Skip to content

Instantly share code, notes, and snippets.

@sesh
Created August 4, 2015 22:39
Show Gist options
  • Select an option

  • Save sesh/9c053497d760b2e3943f to your computer and use it in GitHub Desktop.

Select an option

Save sesh/9c053497d760b2e3943f to your computer and use it in GitHub Desktop.
Django Form Defaults
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