Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom fields not created on objects if Django ORM methods are used #3312

Closed
phoenixswiss opened this issue Feb 14, 2023 · 5 comments · Fixed by #4123
Closed

Custom fields not created on objects if Django ORM methods are used #3312

phoenixswiss opened this issue Feb 14, 2023 · 5 comments · Fixed by #4123
Labels
question Further information is requested type: bug Something isn't working as expected

Comments

@phoenixswiss
Copy link

phoenixswiss commented Feb 14, 2023

Environment

  • Nautobot version (Docker tag too if applicable): 1.5.10
  • Python version: 3.8.10
  • Database platform, version: postgresql, Ubuntu 12.12-0ubuntu0.20.04.1
  • Middleware(s): -

Steps to Reproduce

  1. Create a custom field of any type with ("") or without (null) as default value and assign it to the object type interface.
  2. Create a new job and in that job create a new interface for an existing device using Django ORM methods:
nautobot_ip_interface = Interface(
    device=device,
    name=ip_interface_name,
    type=InterfaceTypeChoices.TYPE_VIRTUAL,
    status=status_active,
    label=ip_interface_name,
    mgmt_only=mgmt_only,
    enabled=True,
    untagged_vlan=untagged_vlan,
    mode="access",
)
nautobot_ip_interface.validated_save()
  1. List the custom field keys by calling nautobot_ip_interface.custom_field_data.keys() or navigate to the Device Change Log site in Nautobot and open the interface created entry for the interface, which you have created in step 2. Under Difference note the value Null and the new value (JSON) with the property
 "custom_fields": { 
       "your_custom_field": null,
  }
  1. On the right side of this change log page under Object Data the custom field section your_custom_field is missing.

Expected Behavior

All custom fields should be created with default values or null on an object if Django ORM methods are used.

Observed Behavior

None custom fields are created.

If you add the custom field data manually before saving, the field will be created:

nautobot_ip_interface.custom_field_data["your_custom_field"] = ""
nautobot_ip_interface.validated_save()

If pynautobot (REST-API) is used, the custom fields will be created during object creation.

This issue has been discussed here: #3298 (reply in thread)

@bryanculver
Copy link
Member

@phoenixswiss Do you have a celery worker running? And if so after you save the object can you run a refresh_from_db after waiting a minute after the object save?

@bryanculver bryanculver added type: bug Something isn't working as expected question Further information is requested labels Feb 14, 2023
@phoenixswiss
Copy link
Author

@bryanculver Yes, I have celery worker running. Saving the object and waiting 60 seconds and then refreshing the object from db had the same effect.

@bryanculver
Copy link
Member

@bryanculver Yes, I have celery worker running. Saving the object and waiting 60 seconds and then refreshing the object from db had the same effect.

Have you tried this outside a job as well?

@phoenixswiss
Copy link
Author

phoenixswiss commented Feb 15, 2023

@bryanculver Yes, I tested it outside a job:

>>> device = Device.objects.get(name="testdevice")
>>> ip_interface_name = "test"
>>> status_active = Status.objects.get(slug="active")
>>> nautobot_ip_interface = Interface(
...         device=device,
...         name=ip_interface_name,
...         type="virtual",
...         status=status_active,
...         label=ip_interface_name,
...         mgmt_only=False,
...         enabled=True,
...         mode="access",
... )
>>> nautobot_ip_interface.validated_save()
>>> nautobot_ip_interface.custom_field_data.keys()
dict_keys([])
>>> time.sleep(60) 
>>> nautobot_ip_interface.refresh_from_db()
>>> nautobot_ip_interface.custom_field_data.keys()
dict_keys([])

@bryanculver
Copy link
Member

Thanks for the extra extra confirmation @phoenixswiss!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Further information is requested type: bug Something isn't working as expected
Projects
No open projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants