Support Creating Custom Fields via the API #993

Closed
opened 2025-12-29 16:27:38 +01:00 by adam · 7 comments
Owner

Originally created by @DerekTBrown on GitHub (May 24, 2017).

I am creating an application which requires the use of several custom fields within NetBox. It would be nice to be able to automate the creation of these custom fields upon installation. Could this be added to the v2 API?

Originally created by @DerekTBrown on GitHub (May 24, 2017). I am creating an application which requires the use of several custom fields within NetBox. It would be nice to be able to automate the creation of these custom fields upon installation. Could this be added to the v2 API?
adam closed this issue 2025-12-29 16:27:38 +01:00
Author
Owner

@jeremystretch commented on GitHub (May 24, 2017):

The creation or modification of custom fields is analogous to modifying the database schema and should only be done with great care. It should also be a very infrequent operation. As such, I believe it's appropriate to restrict control over custom fields to the admin UI.

@jeremystretch commented on GitHub (May 24, 2017): The creation or modification of custom fields is analogous to modifying the database schema and should only be done with great care. It should also be a very infrequent operation. As such, I believe it's appropriate to restrict control over custom fields to the admin UI.
Author
Owner

@DerekTBrown commented on GitHub (May 26, 2017):

From an application design perspective, this is very limiting. I have to ask my users to go into the NetBox panel and add these custom fields (there are a fair number) manually, as opposed to just verifying they exist on installation.

@DerekTBrown commented on GitHub (May 26, 2017): From an application design perspective, this is very limiting. I have to ask my users to go into the NetBox panel and add these custom fields (there are a fair number) manually, as opposed to just verifying they exist on installation.
Author
Owner

@jeremystretch commented on GitHub (May 31, 2017):

If this is just for deployment purposes, you're probably better off scripting out the creation of custom fields using the Django shell (./manage.py shell). For example, to create a custom text field for devices:

from django.contrib.contenttypes.models import ContentType
from dcim.models import Device
from extras.models import CF_TYPE_TEXT, CustomField

new_field = CustomField(type=CF_TYPE_TEXT, name='my_field', description='My custom field')
new_field.save()

content_type = content_type=ContentType.objects.get_for_model(Device)
new_field.obj_type.add(content_type)
@jeremystretch commented on GitHub (May 31, 2017): If this is just for deployment purposes, you're probably better off scripting out the creation of custom fields using the Django shell (`./manage.py shell`). For example, to create a custom text field for devices: ```python from django.contrib.contenttypes.models import ContentType from dcim.models import Device from extras.models import CF_TYPE_TEXT, CustomField new_field = CustomField(type=CF_TYPE_TEXT, name='my_field', description='My custom field') new_field.save() content_type = content_type=ContentType.objects.get_for_model(Device) new_field.obj_type.add(content_type) ```
Author
Owner

@DerekTBrown commented on GitHub (May 31, 2017):

@jeremystretch out of curiosity- is it possible to do the same thing with creating API Tokens?

@DerekTBrown commented on GitHub (May 31, 2017): @jeremystretch out of curiosity- is it possible to do the same thing with creating API Tokens?
Author
Owner

@jeremystretch commented on GitHub (May 31, 2017):

It's possible to do that with everything in NetBox; it's just the built-in Django shell. At some point I'd like to document it within the context of NetBox, but I want to develop a wrapper that preloads everything for convenience first.

Anyway, here's an example for creating tokens:

from django.contrib.auth.models import User
from users.models import Token

usernames = ['tom', 'dick', 'harry']
for name in usernames:
    try:
        # Keys will be generated randomly if not specified
        t = Token(user=User.objects.get(username=name))
        t.save()
        print("Created token {} for {}".format(t.key, t.user))
    except User.DoesNotExist:
        print("User {} not found!")

Does this approach work for you as a substitute for extending the API?

@jeremystretch commented on GitHub (May 31, 2017): It's possible to do that with everything in NetBox; it's just the built-in Django shell. At some point I'd like to document it within the context of NetBox, but I want to develop a wrapper that preloads everything for convenience first. Anyway, here's an example for creating tokens: ```python from django.contrib.auth.models import User from users.models import Token usernames = ['tom', 'dick', 'harry'] for name in usernames: try: # Keys will be generated randomly if not specified t = Token(user=User.objects.get(username=name)) t.save() print("Created token {} for {}".format(t.key, t.user)) except User.DoesNotExist: print("User {} not found!") ``` Does this approach work for you as a substitute for extending the API?
Author
Owner

@DerekTBrown commented on GitHub (Jun 1, 2017):

Its good enough for automated testing, which was the real need. It would definitely be a nicety in the future to automate other installations as well.

@DerekTBrown commented on GitHub (Jun 1, 2017): Its good enough for automated testing, which was the real need. It would definitely be a nicety in the future to automate other installations as well.
Author
Owner

@DerekTBrown commented on GitHub (Jun 5, 2017):

@jeremystretch just for future use, the code block for making custom fields needs to verify the custom field doesn't already exist

@DerekTBrown commented on GitHub (Jun 5, 2017): @jeremystretch just for future use, the code block for making custom fields needs to verify the custom field doesn't already exist
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#993