django-model-utils icon indicating copy to clipboard operation
django-model-utils copied to clipboard

AttributeError: 'DescriptorWrapper' object has no attribute 'get_queryset'

Open al-muammar opened this issue 4 years ago • 3 comments

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()

al-muammar avatar Nov 10 '21 17:11 al-muammar

Also happens with B.x.field, the wrapper breaks anything that introspects models

stevelacey avatar Mar 29 '22 13:03 stevelacey

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

stevelacey avatar Mar 29 '22 13:03 stevelacey

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)

stevelacey avatar Mar 31 '22 13:03 stevelacey