AttributeError returned when Selection Custom Field in POST body #4006

Closed
opened 2025-12-29 18:32:35 +01:00 by adam · 7 comments
Owner

Originally created by @WilliamMarti on GitHub (Aug 21, 2020).

Environment

  • Python version: 3.6.9
  • NetBox version: 2.9.2

Steps to Reproduce

  1. POST request to the /api/ipam/prefixes/{id}/available-ips/ endpoint
  2. POST request has a Selection custom field for example:
"custom_fields": {
    "ipam_DNSZone": 20,
}

Expected an IP address to be assigned. I have my production Netbox instance running v2.8.8 where this same request works. Note that I had a test instance running v2.9-beta1 where I believe this was working as well. This request is also working when I leave out my custom field listed above altogether.

Instead I am getting the error:

<class 'AttributeError'>

Got AttributeError when attempting to get a value for field `value` on serializer `CustomFieldChoiceSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `int` instance.
Original exception text was: 'int' object has no attribute 'pk'.

If there is anything else I can provide let me know

Originally created by @WilliamMarti on GitHub (Aug 21, 2020). ### Environment * Python version: 3.6.9 * NetBox version: 2.9.2 ### Steps to Reproduce 1. POST request to the `/api/ipam/prefixes/{id}/available-ips/` endpoint 2. POST request has a **Selection** custom field for example: ``` "custom_fields": { "ipam_DNSZone": 20, } ``` Expected an IP address to be assigned. I have my production Netbox instance running v2.8.8 where this same request works. Note that I had a test instance running v2.9-beta1 where I believe this was working as well. This request is also working when I leave out my custom field listed above altogether. Instead I am getting the error: ``` <class 'AttributeError'> Got AttributeError when attempting to get a value for field `value` on serializer `CustomFieldChoiceSerializer`. The serializer field might be named incorrectly and not match any attribute or key on the `int` instance. Original exception text was: 'int' object has no attribute 'pk'. ``` If there is anything else I can provide let me know
adam added the type: bug label 2025-12-29 18:32:35 +01:00
adam closed this issue 2025-12-29 18:32:35 +01:00
Author
Owner

@DanSheps commented on GitHub (Aug 23, 2020):

Since you are using custom fields, can you please post the full steps to reproduce this, not just the post?

@DanSheps commented on GitHub (Aug 23, 2020): Since you are using custom fields, can you please post the full steps to reproduce this, not just the post?
Author
Owner

@WilliamMarti commented on GitHub (Aug 24, 2020):

I am running into this error when hitting the REST API, so I am just using Insomnia to make the above POST.

Here is a python example of hitting the API that would generate the error (few things sanitized). Only requirement would be having a custom field for the IP Address object type that is a Selection.

import requests

url = "https://netbox.example.com/api/ipam/prefixes/6794/available-ips/"

payload = "{\n\t\"description\": \"testserver1\",\n\t\"custom_fields\": {\n\t\t\"selection_custom_field\": 20\n\t}\n}"
headers = {
    'authorization': "Token token",
    'content-type': "application/json",
    'accept': "application/json"
    }

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)
@WilliamMarti commented on GitHub (Aug 24, 2020): I am running into this error when hitting the REST API, so I am just using Insomnia to make the above POST. Here is a python example of hitting the API that would generate the error (few things sanitized). Only requirement would be having a custom field for the IP Address object type that is a Selection. ``` import requests url = "https://netbox.example.com/api/ipam/prefixes/6794/available-ips/" payload = "{\n\t\"description\": \"testserver1\",\n\t\"custom_fields\": {\n\t\t\"selection_custom_field\": 20\n\t}\n}" headers = { 'authorization': "Token token", 'content-type': "application/json", 'accept': "application/json" } response = requests.request("POST", url, data=payload, headers=headers) print(response.text) ```
Author
Owner

@jeremystretch commented on GitHub (Aug 24, 2020):

This report needs to include

  1. The complete list of steps someone else can take to reproduce the problem, beginning with the creation of the custom field; and
  2. The raw REST API request being sent (e.g. using curl)
@jeremystretch commented on GitHub (Aug 24, 2020): This report needs to include 1. The complete list of steps someone else can take to reproduce the problem, beginning with the creation of the custom field; and 2. The raw REST API request being sent (e.g. using `curl`)
Author
Owner

@WilliamMarti commented on GitHub (Sep 1, 2020):

  1. Create custom field called "bugreport" of type Selection with options apple, orange, banana
  2. Find the IDs of the choices we just made
curl --request GET \
  --url https://netbox.example.com/api/extras/_custom_field_choices/ \
  --header 'accept: application/json' \
  --header 'authorization: Token $token' \
  --header 'content-type: application/json' \

In my case this is: 67,69,68

  1. Create a fresh Prefix to assign an IP in:
curl --request POST \
  --url https://netbox.example.com/api/ipam/prefixes/ \
  --header 'accept: application/json' \
  --header 'authorization: Token $token' \
  --header 'content-type: application/json' \
  --data '{
  "prefix": "10.10.10.0/24"
}'
  1. Make a request to assign an IP in the above create Prefix, with one of the custom options found above. In this case the Prefix ID is 11913
curl --request POST \
  --url https://netbox.example.com/api/ipam/prefixes/11913/available-ips/ \
  --header 'accept: application/json' \
  --header 'authorization: Token $token' \
  --header 'content-type: application/json' \
  --data '{
  "description": "testserver1",
  "custom_fields": {
    "bugreport": 67
  }
}'

Result--

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Server Error</title>
    <link rel="stylesheet" href="/static/bootstrap-3.4.1-dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/font-awesome-4.7.0/css/font-awesome.min.css">
    <meta charset="UTF-8">
</head>

<body>
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
                <div class="panel panel-danger" style="margin-top: 200px">
                    <div class="panel-heading">
                        <strong>
                            <i class="fa fa-warning"></i>
                            Server Error
                        </strong>
                    </div>
                    <div class="panel-body">
                        
                            <p>
                                There was a problem with your request. Please contact an administrator.
                            </p>
                        
                        <hr />
                        <p>
                            The complete exception is provided below:
                        </p>
<pre><strong>&lt;class &#x27;AttributeError&#x27;&gt;</strong><br />
Got AttributeError when attempting to get a value for field `value` on serializer `CustomFieldChoiceSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `int` instance.
Original exception text was: &#x27;int&#x27; object has no attribute &#x27;pk&#x27;.

Python version: 3.6.9
NetBox version: 2.9.2</pre>
                        <p>
                            If further assistance is required, please post to the <a href="https://groups.google.com/forum/#!forum/netbox-discuss">NetBox mailing list</a>.
                        </p>
                        <div class="text-right">
                            <a href="/" class="btn btn-primary">Home Page</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>

</html>
@WilliamMarti commented on GitHub (Sep 1, 2020): 1. Create custom field called "bugreport" of type Selection with options apple, orange, banana 2. Find the IDs of the choices we just made ``` curl --request GET \ --url https://netbox.example.com/api/extras/_custom_field_choices/ \ --header 'accept: application/json' \ --header 'authorization: Token $token' \ --header 'content-type: application/json' \ ``` In my case this is: 67,69,68 3. Create a fresh Prefix to assign an IP in: ``` curl --request POST \ --url https://netbox.example.com/api/ipam/prefixes/ \ --header 'accept: application/json' \ --header 'authorization: Token $token' \ --header 'content-type: application/json' \ --data '{ "prefix": "10.10.10.0/24" }' ``` 4. Make a request to assign an IP in the above create Prefix, with one of the custom options found above. In this case the Prefix ID is 11913 ``` curl --request POST \ --url https://netbox.example.com/api/ipam/prefixes/11913/available-ips/ \ --header 'accept: application/json' \ --header 'authorization: Token $token' \ --header 'content-type: application/json' \ --data '{ "description": "testserver1", "custom_fields": { "bugreport": 67 } }' ``` Result-- ``` <!DOCTYPE html> <html lang="en"> <head> <title>Server Error</title> <link rel="stylesheet" href="/static/bootstrap-3.4.1-dist/css/bootstrap.min.css"> <link rel="stylesheet" href="/static/font-awesome-4.7.0/css/font-awesome.min.css"> <meta charset="UTF-8"> </head> <body> <div class="container-fluid"> <div class="row"> <div class="col-md-6 col-md-offset-3"> <div class="panel panel-danger" style="margin-top: 200px"> <div class="panel-heading"> <strong> <i class="fa fa-warning"></i> Server Error </strong> </div> <div class="panel-body"> <p> There was a problem with your request. Please contact an administrator. </p> <hr /> <p> The complete exception is provided below: </p> <pre><strong>&lt;class &#x27;AttributeError&#x27;&gt;</strong><br /> Got AttributeError when attempting to get a value for field `value` on serializer `CustomFieldChoiceSerializer`. The serializer field might be named incorrectly and not match any attribute or key on the `int` instance. Original exception text was: &#x27;int&#x27; object has no attribute &#x27;pk&#x27;. Python version: 3.6.9 NetBox version: 2.9.2</pre> <p> If further assistance is required, please post to the <a href="https://groups.google.com/forum/#!forum/netbox-discuss">NetBox mailing list</a>. </p> <div class="text-right"> <a href="/" class="btn btn-primary">Home Page</a> </div> </div> </div> </div> </div> </div> </body> </html> ```
Author
Owner

@jeremystretch commented on GitHub (Sep 1, 2020):

I'm not able to reproduce this issue on v2.9.2. Do you by chance have a webhook defined for the IPAddress model? The error looks similar to #5081.

@jeremystretch commented on GitHub (Sep 1, 2020): I'm not able to reproduce this issue on v2.9.2. Do you by chance have a webhook defined for the IPAddress model? The error looks similar to #5081.
Author
Owner

@WilliamMarti commented on GitHub (Sep 1, 2020):

I do indeed have a webhook for the IPAddress model. Just ran another test of the /ipam/prefixes/11913/available-ips/ endpoint after disabling the webhook, and it worked. Re-enabling the IPAddress webhook I am seeing the same error as above.

@WilliamMarti commented on GitHub (Sep 1, 2020): I do indeed have a webhook for the IPAddress model. Just ran another test of the `/ipam/prefixes/11913/available-ips/` endpoint after disabling the webhook, and it worked. Re-enabling the IPAddress webhook I am seeing the same error as above.
Author
Owner

@jeremystretch commented on GitHub (Sep 1, 2020):

@WilliamMarti Thanks. Definitely sounds like #5081 then. Will be fixed in v2.9.3.

@jeremystretch commented on GitHub (Sep 1, 2020): @WilliamMarti Thanks. Definitely sounds like #5081 then. Will be fixed in v2.9.3.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#4006