Jelajahi Sumber

Fixed default value for boolean fields

Jeremy Stretch 9 tahun lalu
induk
melakukan
6f44f4245e

+ 8 - 1
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:

+ 2 - 2
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={

+ 2 - 1
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: