Creating permission in Custom Script

222 views
Skip to first unread message

Mats Brorson

unread,
May 27, 2021, 10:16:20 AM5/27/21
to NetBox
Trying to create a permission from custom script like this:

        perm = ObjectPermission(
            name = script,
            actions=['run','view','add','change','delete']
        )
        perm.save()
        perm.object_types.set('extras.script')
       
As I understand it I need to use the .set() for adding the object_types property. However, i get the error:

ValueError: Field 'id' expected a number but got 'extras.script'.

Bummer, so the perm.object.set() expects an integer? FIne, but I can't find a way of getting that id. nbshell help just naggs about pizzas and toppings:-)

Anyone?

Mats Brorson

unread,
May 27, 2021, 10:41:03 AM5/27/21
to NetBox
Never mind.... found it by looking into the Django stuff.


       from django.contrib.contenttypes.models import ContentType
       perm = ObjectPermission(
            name = script,
            actions=['run','view','add','change','delete']
        )
        perm.save()
        perm.object_types.add(ContentType.objects.get(app_label='extras', model='script').id)
        perm.save()

Works as it should

Reply all
Reply to author
Forward
0 new messages