Proper django field duplication
This commit is contained in:
parent
33b82af67d
commit
2f607f8271
1 changed files with 17 additions and 2 deletions
|
@ -9,6 +9,21 @@ from django.utils.translation import gettext_lazy as _
|
||||||
from servala.core.models import ServiceInstance
|
from servala.core.models import ServiceInstance
|
||||||
|
|
||||||
|
|
||||||
|
def duplicate_field(field_name, model):
|
||||||
|
# Get the field from the model
|
||||||
|
field = model._meta.get_field(field_name)
|
||||||
|
|
||||||
|
# Create a new field with the same attributes
|
||||||
|
new_field = type(field).__new__(type(field))
|
||||||
|
new_field.__dict__.update(field.__dict__)
|
||||||
|
|
||||||
|
# Ensure the field is not linked to the original model
|
||||||
|
new_field.model = None
|
||||||
|
new_field.auto_created = False
|
||||||
|
|
||||||
|
return new_field
|
||||||
|
|
||||||
|
|
||||||
def generate_django_model(schema, group, version, kind):
|
def generate_django_model(schema, group, version, kind):
|
||||||
"""
|
"""
|
||||||
Generates a virtual Django model from a Kubernetes CRD's OpenAPI v3 schema.
|
Generates a virtual Django model from a Kubernetes CRD's OpenAPI v3 schema.
|
||||||
|
@ -17,8 +32,8 @@ def generate_django_model(schema, group, version, kind):
|
||||||
# defaults = {"apiVersion": f"{group}/{version}", "kind": kind}
|
# defaults = {"apiVersion": f"{group}/{version}", "kind": kind}
|
||||||
|
|
||||||
model_fields = {"__module__": "crd_models"}
|
model_fields = {"__module__": "crd_models"}
|
||||||
for field in ("name", "organization", "context"):
|
for field_name in ("name", "organization", "context"):
|
||||||
model_fields[field] = ServiceInstance._meta.get_field(field)
|
model_fields[field_name] = duplicate_field(field_name, ServiceInstance)
|
||||||
model_fields.update(build_object_fields(spec, "spec"))
|
model_fields.update(build_object_fields(spec, "spec"))
|
||||||
|
|
||||||
meta_class = type("Meta", (), {"app_label": "crd_models"})
|
meta_class = type("Meta", (), {"app_label": "crd_models"})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue