django-model-utils
django-model-utils copied to clipboard
AttributeError: 'DescriptorWrapper' object has no attribute 'get_queryset'
Problem
When accessing model's field, DescriptorWrapper returns itself as an object, which breaks the logic. See more in examples.
Environment
- Django Model Utils version: 4.1.1
- Django version: 3.2.8
- Python version: 3.9.1
- Other libraries used, if any:
Code examples
class A(models.Model):
pass
class B(models.Model):
x = models.ForeignKey(A, on_delete=models.CASCADE)
tracker = FieldTracker(fields=["x"])
# This throws
B.x.get_queryset()
Also happens with B.x.field, the wrapper breaks anything that introspects models
This works fwiw
from model_utils.tracker import DescriptorWrapper
@property
def field(self):
return self.descriptor.field
@property
def get_queryset(self):
return self.descriptor.get_queryset
DescriptorWrapper.field = field
DescriptorWrapper.get_queryset = get_queryset
I am really not sure what's going on with DescriptorWrapper or why it doesn't quack like a duck if it's wrapping fields.
I am kind of surprised this doesn't cause more issues, you'd think lots of things would access .field.
@lucaswiman any suggestions? would this be a reasonable addition to DescriptorWrapper?
def __getattr__(self, attr):
return getattr(self.descriptor, attr)