can_add() template filter raises exception when passing a model #9188

Closed
opened 2025-12-29 20:46:46 +01:00 by adam · 0 comments
Owner

Originally created by @jeremystretch on GitHub (Feb 2, 2024).

Originally assigned to: @jeremystretch on GitHub.

Deployment Type

NetBox Cloud

NetBox Version

v3.7.1

Python Version

3.11

Steps to Reproduce

There is an issue with how the can_add() filter evaluates permissions. It should expect to receive a model rather than a specific instance (as an existing instance obviously cannot be create again). The following template code fails:

{% load perms %}
{% if request.user|can_add:model %}

Below is a reconstruction using the Python shell:

>>> from utilities.templatetags.perms import can_add
>>> user=User.objects.get(username='bob')
>>> user.has_perm('dcim.add_poweroutlet')
True
>>> can_add(user, PowerOutlet)
Traceback (most recent call last):
  File "/home/jstretch/projects/netbox/venv/lib/python3.10/site-packages/django/db/models/fields/__init__.py", line 2053, in get_prep_value
    return int(value)
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'property'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  ...
  File "/home/jstretch/projects/netbox/venv/lib/python3.10/site-packages/django/db/models/fields/__init__.py", line 2055, in get_prep_value
    raise e.__class__(
TypeError: Field 'id' expected a number but got <property object at 0x7fa360779760>.

Expected Behavior

Passing a model to the can_add() filter should successfully evaluate the user's permission to create a new instance of that model.

Observed Behavior

A TypeError exception is raised.

Originally created by @jeremystretch on GitHub (Feb 2, 2024). Originally assigned to: @jeremystretch on GitHub. ### Deployment Type NetBox Cloud ### NetBox Version v3.7.1 ### Python Version 3.11 ### Steps to Reproduce There is an issue with how the `can_add()` filter evaluates permissions. It should expect to receive a model rather than a specific instance (as an existing instance obviously cannot be create again). The following template code fails: ``` {% load perms %} {% if request.user|can_add:model %} ``` Below is a reconstruction using the Python shell: ```python >>> from utilities.templatetags.perms import can_add >>> user=User.objects.get(username='bob') >>> user.has_perm('dcim.add_poweroutlet') True >>> can_add(user, PowerOutlet) Traceback (most recent call last): File "/home/jstretch/projects/netbox/venv/lib/python3.10/site-packages/django/db/models/fields/__init__.py", line 2053, in get_prep_value return int(value) TypeError: int() argument must be a string, a bytes-like object or a real number, not 'property' The above exception was the direct cause of the following exception: Traceback (most recent call last): ... File "/home/jstretch/projects/netbox/venv/lib/python3.10/site-packages/django/db/models/fields/__init__.py", line 2055, in get_prep_value raise e.__class__( TypeError: Field 'id' expected a number but got <property object at 0x7fa360779760>. ``` ### Expected Behavior Passing a model to the `can_add()` filter should successfully evaluate the user's permission to create a new instance of that model. ### Observed Behavior A `TypeError` exception is raised.
adam added the type: bugstatus: acceptedseverity: low labels 2025-12-29 20:46:46 +01:00
adam closed this issue 2025-12-29 20:46:46 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#9188