From 6f44f4245ec302ad672709216895fef3dd9a81d5 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 18 Aug 2016 11:44:40 -0400 Subject: [PATCH] Fixed default value for boolean fields --- netbox/extras/forms.py | 9 ++++++++- netbox/extras/migrations/0002_custom_fields.py | 4 ++-- netbox/extras/models.py | 3 ++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/netbox/extras/forms.py b/netbox/extras/forms.py index 20b52589d..d62caf2aa 100644 --- a/netbox/extras/forms.py +++ b/netbox/extras/forms.py @@ -25,7 +25,14 @@ def get_custom_fields_for_model(content_type, bulk_editing=False): (True, 'True'), (False, 'False'), ) - field = forms.NullBooleanField(required=cf.required, widget=forms.Select(choices=choices)) + if cf.default.lower() in ['true', 'yes', '1']: + initial = True + elif cf.default.lower() in ['false', 'no', '0']: + initial = False + else: + initial = None + field = forms.NullBooleanField(required=cf.required, initial=initial, + widget=forms.Select(choices=choices)) # Date elif cf.type == CF_TYPE_DATE: diff --git a/netbox/extras/migrations/0002_custom_fields.py b/netbox/extras/migrations/0002_custom_fields.py index e43067610..97d0b7112 100644 --- a/netbox/extras/migrations/0002_custom_fields.py +++ b/netbox/extras/migrations/0002_custom_fields.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.10 on 2016-08-17 18:42 +# Generated by Django 1.10 on 2016-08-18 15:43 from __future__ import unicode_literals from django.db import migrations, models @@ -23,7 +23,7 @@ class Migration(migrations.Migration): ('label', models.CharField(blank=True, help_text=b"Name of the field as displayed to users (if not provided, the field's name will be used)", max_length=50)), ('description', models.CharField(blank=True, max_length=100)), ('required', models.BooleanField(default=False, help_text=b'Determines whether this field is required when creating new objects or editing an existing object.')), - ('default', models.CharField(blank=True, help_text=b'Default value for the field. N/A for selection fields.', max_length=100)), + ('default', models.CharField(blank=True, help_text=b'Default value for the field. Use "true" or "false" for booleans. N/A for selection fields.', max_length=100)), ('obj_type', models.ManyToManyField(help_text=b'The object(s) to which this field applies.', related_name='custom_fields', to='contenttypes.ContentType', verbose_name=b'Object(s)')), ], options={ diff --git a/netbox/extras/models.py b/netbox/extras/models.py index 0a038e657..1965e5f72 100644 --- a/netbox/extras/models.py +++ b/netbox/extras/models.py @@ -90,7 +90,8 @@ class CustomField(models.Model): description = models.CharField(max_length=100, blank=True) required = models.BooleanField(default=False, help_text="Determines whether this field is required when creating " "new objects or editing an existing object.") - default = models.CharField(max_length=100, blank=True, help_text="Default value for the field. N/A for selection " + default = models.CharField(max_length=100, blank=True, help_text="Default value for the field. Use \"true\" or " + "\"false\" for booleans. N/A for selection " "fields.") class Meta: