Custom form configuration #268
3 changed files with 121 additions and 0 deletions
|
|
@ -318,6 +318,9 @@ class CustomFormMixin(FormGeneratorMixin):
|
||||||
if validators:
|
if validators:
|
||||||
field.validators.extend(validators)
|
field.validators.extend(validators)
|
||||||
|
|
||||||
|
if "default_value" in field_config and field.initial is None:
|
||||||
|
field.initial = field_config["default_value"]
|
||||||
|
|
||||||
field.controlplane_field_mapping = field_name
|
field.controlplane_field_mapping = field_name
|
||||||
|
|
||||||
def get_fieldsets(self):
|
def get_fieldsets(self):
|
||||||
|
|
|
||||||
|
|
@ -92,6 +92,10 @@
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"enum": ["email", "fqdn", "url", "ipv4", "ipv6"]
|
"enum": ["email", "fqdn", "url", "ipv4", "ipv6"]
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"default_value": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "Default value for the field when creating new instances"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -519,3 +519,117 @@ def test_number_field_min_max_sets_widget_attributes():
|
||||||
isinstance(v, MaxValueValidator) and v.limit_value == 65535
|
isinstance(v, MaxValueValidator) and v.limit_value == 65535
|
||||||
for v in port_validators
|
for v in port_validators
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_default_value_for_all_field_types():
|
||||||
|
|
||||||
|
class TestModel(models.Model):
|
||||||
|
name = models.CharField(max_length=100)
|
||||||
|
description = models.TextField()
|
||||||
|
port = models.IntegerField()
|
||||||
|
environment = models.CharField(
|
||||||
|
max_length=20,
|
||||||
|
choices=[
|
||||||
|
("dev", "Development"),
|
||||||
|
("staging", "Staging"),
|
||||||
|
("prod", "Production"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
monitoring_enabled = models.BooleanField()
|
||||||
|
tags = models.JSONField()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
app_label = "test"
|
||||||
|
|
||||||
|
form_config = {
|
||||||
|
"fieldsets": [
|
||||||
|
{
|
||||||
|
"fields": [
|
||||||
|
{
|
||||||
|
"type": "text",
|
||||||
|
"label": "Name",
|
||||||
|
"controlplane_field_mapping": "name",
|
||||||
|
"default_value": "default-name",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "textarea",
|
||||||
|
"label": "Description",
|
||||||
|
"controlplane_field_mapping": "description",
|
||||||
|
"default_value": "Default description text",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "number",
|
||||||
|
"label": "Port",
|
||||||
|
"controlplane_field_mapping": "port",
|
||||||
|
"default_value": "8080",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "choice",
|
||||||
|
"label": "Environment",
|
||||||
|
"controlplane_field_mapping": "environment",
|
||||||
|
"default_value": "dev",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "checkbox",
|
||||||
|
"label": "Enable Monitoring",
|
||||||
|
"controlplane_field_mapping": "monitoring_enabled",
|
||||||
|
"default_value": "true",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "array",
|
||||||
|
"label": "Tags",
|
||||||
|
"controlplane_field_mapping": "tags",
|
||||||
|
"default_value": "tag1,tag2,tag3",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
form_class = generate_custom_form_class(form_config, TestModel)
|
||||||
|
form = form_class()
|
||||||
|
|
||||||
|
assert form.fields["name"].initial == "default-name"
|
||||||
|
assert form.fields["description"].initial == "Default description text"
|
||||||
|
assert form.fields["port"].initial == "8080"
|
||||||
|
assert form.fields["environment"].initial == "dev"
|
||||||
|
assert form.fields["monitoring_enabled"].initial == "true"
|
||||||
|
assert form.fields["tags"].initial == "tag1,tag2,tag3"
|
||||||
|
|
||||||
|
|
||||||
|
def test_default_value_not_override_existing_instance():
|
||||||
|
|
||||||
|
class TestModel(models.Model):
|
||||||
|
name = models.CharField(max_length=100)
|
||||||
|
port = models.IntegerField()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
app_label = "test"
|
||||||
|
|
||||||
|
form_config = {
|
||||||
|
"fieldsets": [
|
||||||
|
{
|
||||||
|
"fields": [
|
||||||
|
{
|
||||||
|
"type": "text",
|
||||||
|
"label": "Name",
|
||||||
|
"controlplane_field_mapping": "name",
|
||||||
|
"default_value": "default-name",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "number",
|
||||||
|
"label": "Port",
|
||||||
|
"controlplane_field_mapping": "port",
|
||||||
|
"default_value": "8080",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
instance = TestModel(name="existing-name", port=3000)
|
||||||
|
form_class = generate_custom_form_class(form_config, TestModel)
|
||||||
|
form = form_class(instance=instance)
|
||||||
|
|
||||||
|
assert form.initial["name"] == "existing-name"
|
||||||
|
assert form.initial["port"] == 3000
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue