Skip to content

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

@jeremystretch

Description

@jeremystretch

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.

Metadata

Metadata

Assignees

Labels

severity: lowDoes not significantly disrupt application functionality, or a workaround is availablestatus: acceptedThis issue has been accepted for implementationtype: bugA confirmed report of unexpected behavior in the application

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions