andrewgodwin / urlman

Apache License 2.0
117 stars 8 forks source link

AttributeError: 'UrlManField' object has no attribute 'allow_null' #16

Closed bluesurfer closed 4 months ago

bluesurfer commented 4 months ago

When using UrlManField with drf-spectacular I get AttributeError: 'UrlManField' object has no attribute 'allow_null' when visiting the docs.

rixx commented 4 months ago

Yup, that one's on me. Fixed in #17, just needs a review/merge/release by @andrewgodwin.

bluesurfer commented 4 months ago

@rixx I tried exactly that fix. But I got a different attribute error in the to_representation method. This is what worked for me:

class UrlManField(DictField):
    """Serializer class for Django Restframework."""

    def __init__(self, urls, attribute="urls", full=True, *args, **kwargs):
        # Temporary fix for https://github.com/andrewgodwin/urlman/issues/16
        super().__init__(
            *args,
            read_only=True,
            write_only=False,
            label=None,
            source='*',
            **kwargs
        )
        self.urls = urls
        self.url_attribute = attribute
        self.full = full
andrewgodwin commented 4 months ago

Hmm, I just merged #17 but if it's not what works we need to change it - what was the exact error you got with that new code?

rixx commented 4 months ago

But I got a different attribute error in the to_representation method.

Oh, that would have been good to know beforehand. I tried to test it with the project I use urlman in, but it next failed for me with a problem unrelated to urlman (I use partials in a serializer and it fails to deal with that), so I didn't look into it further, as the reported bug went away.

Out of curiosity, I fixed the problem, and can't reproduce the error (though the schema shows the URL field to be a string). I'll open a PR with a variation of your solution.

bluesurfer commented 4 months ago

@rixx @andrewgodwin if you apply the fix of #17 then url_class becomes a dictionary (don't know why). You should therefore get AttributeError: 'dict' object has no attribute 'view' where view is a url listed in urls.