You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Nautobot version (Docker tag too if applicable): 2.0.0b1
Python version: 3.9
Database platform, version: n/a
Middleware(s): n/a
Steps to Reproduce
Replace IPAddress.natural_key_field_names with ["parent__namespace__name", "host"]
Get an instance of an IPAddress
Attempt to call IPAddress.objects.get_by_natural_key(*instance.natural_key()) to perform a symmetric lookup
Expected Behavior
Should return the same instance that was used to make the call.
Observed Behavior
A FieldDoesNotExist error is raised:
In [1]: instance=IPAddress.objects.first()
In [2]: instance.natural_key()
---------------------------------------------------------------------------KeyErrorTraceback (mostrecentcalllast)
File~/Library/Caches/pypoetry/virtualenvs/nautobot-b2ttWva6-py3.10/lib/python3.10/site-packages/django/db/models/options.py:608, inOptions.get_field(self, field_name)
605try:
606# Retrieve field instance by name from cached or just-computed607# field map.-->608returnself.fields_map[field_name]
609exceptKeyError:
KeyError: 'parent__namespace__name'Duringhandlingoftheaboveexception, anotherexceptionoccurred:
FieldDoesNotExistTraceback (mostrecentcalllast)
CellIn[2], line1---->1instance.natural_key()
File~/sandbox/src/nautobot/nautobot/core/models/__init__.py:90, inBaseModel.natural_key(self)
83""" 84 Smarter default implementation of natural key construction. 85 86 1. Handles nullable foreign keys (https://github.com/wq/django-natural-keys/issues/18) 87 2. Handles variadic natural-keys (e.g. Location model - [name, parent__name, parent__parent__name, ...].) 88 """89vals= []
--->90forlookupsin [lookup.split("__") forlookupinself.natural_key_field_lookups]:
91val=self92forlookupinlookups:
File~/Library/Caches/pypoetry/virtualenvs/nautobot-b2ttWva6-py3.10/lib/python3.10/site-packages/django/utils/functional.py:61, inclassproperty.__get__(self, instance, cls)
60def__get__(self, instance, cls=None):
--->61returnself.fget(cls)
File~/sandbox/src/nautobot/nautobot/core/models/__init__.py:156, inBaseModel.natural_key_field_lookups(cls)
154natural_key_field_lookups= []
155forfield_nameinnatural_key_field_names:
-->156field=cls._meta.get_field(field_name)
157ifgetattr(field, "remote_field", None) isNone:
158# Not a related field, so the field name is the field lookup159natural_key_field_lookups.append(field_name)
File~/Library/Caches/pypoetry/virtualenvs/nautobot-b2ttWva6-py3.10/lib/python3.10/site-packages/django/db/models/options.py:610, inOptions.get_field(self, field_name)
608returnself.fields_map[field_name]
609exceptKeyError:
-->610raiseFieldDoesNotExist("%s has no field named '%s'"% (self.object_name, field_name))
FieldDoesNotExist: IPAddresshasnofieldnamed'parent__namespace__name'
Additionally if you instead try to overload natural_key_field_lookups (vs. natural_key_field_names), the call to instance.natural_key() succeeds, but calls to IPAddress.objects.get_by_natural_key() is out of phase and results in a DoesNotExist error:
In [1]: instance=IPAddress.objects.first()
In [2]: instance.natural_key()
Out[2]: ['Global', '10.0.0.0']
In [3]: IPAddress.objects.get_by_natural_key(*instance.natural_key())
---------------------------------------------------------------------------DoesNotExistTraceback (mostrecentcalllast)
File~/sandbox/src/nautobot/nautobot/core/models/managers.py:42, inBaseManager.get_by_natural_key(self, *args)
41try:
--->42kwargs[field_name] =related_model.objects.get_by_natural_key(*related_values)
43exceptrelated_model.DoesNotExistasexc:
File~/sandbox/src/nautobot/nautobot/core/models/managers.py:46, inBaseManager.get_by_natural_key(self, *args)
44raiseself.model.DoesNotExist() fromexc--->46returnself.get(**kwargs)
File~/Library/Caches/pypoetry/virtualenvs/nautobot-b2ttWva6-py3.10/lib/python3.10/site-packages/django/db/models/manager.py:85, inBaseManager._get_queryset_methods.<locals>.create_method.<locals>.manager_method(self, *args, **kwargs)
84defmanager_method(self, *args, **kwargs):
--->85returngetattr(self.get_queryset(), name)(*args, **kwargs)
File~/sandbox/src/nautobot/nautobot/ipam/querysets.py:261, inPrefixQuerySet.get(self, prefix, *args, **kwargs)
260kwargs["broadcast"] =last_ip-->261returnsuper().get(*args, **kwargs)
File~/Library/Caches/pypoetry/virtualenvs/nautobot-b2ttWva6-py3.10/lib/python3.10/site-packages/django/db/models/query.py:435, inQuerySet.get(self, *args, **kwargs)
434ifnotnum:
-->435raiseself.model.DoesNotExist(
436"%s matching query does not exist."%437self.model._meta.object_name438 )
439raiseself.model.MultipleObjectsReturned(
440'get() returned more than one %s -- it returned %s!'% (
441self.model._meta.object_name,
442numifnotlimitornum<limitelse'more than %s'% (limit-1),
443 )
444 )
DoesNotExist: Prefixmatchingquerydoesnotexist.
Theaboveexceptionwasthedirectcauseofthefollowingexception:
DoesNotExistTraceback (mostrecentcalllast)
CellIn[3], line1---->1IPAddress.objects.get_by_natural_key(*instance.natural_key())
File~/sandbox/src/nautobot/nautobot/core/models/managers.py:44, inBaseManager.get_by_natural_key(self, *args)
42kwargs[field_name] =related_model.objects.get_by_natural_key(*related_values)
43exceptrelated_model.DoesNotExistasexc:
--->44raiseself.model.DoesNotExist() fromexc46returnself.get(**kwargs)
DoesNotExist:
The text was updated successfully, but these errors were encountered:
Environment
Steps to Reproduce
IPAddress.natural_key_field_names
with["parent__namespace__name", "host"]
IPAddress
IPAddress.objects.get_by_natural_key(*instance.natural_key())
to perform a symmetric lookupExpected Behavior
Should return the same instance that was used to make the call.
Observed Behavior
A
FieldDoesNotExist
error is raised:Additionally if you instead try to overload
natural_key_field_lookups
(vs.natural_key_field_names
), the call toinstance.natural_key()
succeeds, but calls toIPAddress.objects.get_by_natural_key()
is out of phase and results in aDoesNotExist
error:The text was updated successfully, but these errors were encountered: