Build custom object instantiation
This commit is contained in:
parent
f03940fe61
commit
9830eebcda
1 changed files with 28 additions and 1 deletions
|
@ -9,6 +9,18 @@ from django.utils.translation import gettext_lazy as _
|
||||||
from servala.core.models import ServiceInstance
|
from servala.core.models import ServiceInstance
|
||||||
|
|
||||||
|
|
||||||
|
class CRDModel(models.Model):
|
||||||
|
"""Base class for all virtual CRD models"""
|
||||||
|
|
||||||
|
def __init__(self, **kwargs):
|
||||||
|
if spec := kwargs.pop("spec", None):
|
||||||
|
kwargs.update(unnest_data({"spec": spec}))
|
||||||
|
super().__init__(**kwargs)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
abstract = True
|
||||||
|
|
||||||
|
|
||||||
def duplicate_field(field_name, model):
|
def duplicate_field(field_name, model):
|
||||||
# Get the field from the model
|
# Get the field from the model
|
||||||
field = model._meta.get_field(field_name)
|
field = model._meta.get_field(field_name)
|
||||||
|
@ -47,7 +59,7 @@ def generate_django_model(schema, group, version, kind):
|
||||||
|
|
||||||
# create the model class
|
# create the model class
|
||||||
model_name = kind
|
model_name = kind
|
||||||
model_class = type(model_name, (models.Model,), model_fields)
|
model_class = type(model_name, (CRDModel,), model_fields)
|
||||||
return model_class
|
return model_class
|
||||||
|
|
||||||
|
|
||||||
|
@ -138,6 +150,21 @@ def get_django_field(
|
||||||
return models.CharField(max_length=255, **kwargs)
|
return models.CharField(max_length=255, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
def unnest_data(data):
|
||||||
|
result = {}
|
||||||
|
|
||||||
|
def _flatten_dict(d, parent_key=""):
|
||||||
|
for key, value in d.items():
|
||||||
|
new_key = f"{parent_key}.{key}" if parent_key else key
|
||||||
|
if isinstance(value, dict):
|
||||||
|
_flatten_dict(value, new_key)
|
||||||
|
else:
|
||||||
|
result[new_key] = value
|
||||||
|
|
||||||
|
_flatten_dict(data)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
class CrdModelFormMixin:
|
class CrdModelFormMixin:
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue