Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unsaved File Objects may not have a .url attribute. #2759

Closed
wants to merge 1 commit into from

Conversation

ar45
Copy link

@ar45 ar45 commented Mar 24, 2015

I'm unsure how this should behave.
basically accessing the serializer.data with a FileField value of InMemoryUploadedFile raises FileField 'InMemoryUploadedFile' object has no attribute 'url'

This example does not provide any special use case, but I guess this should always provide the same behavior no matter what the setting of 'UPLOADED_FILES_USE_URL': False|True

class FileSerializer(serializers.Serializer):
   file = serializers.FileField()

class MyView(views.APIView):
    def post(request, **kwargs):
        s = FileSerializer(data=request.data)
        if s.is_valid():
            return Response(data=serializer.data)
        return Response(data=serializer.errors)

AttributeError at /api/files
'InMemoryUploadedFile' object has no attribute 'url'

@ar45
Copy link
Author

ar45 commented Mar 24, 2015

Any explanation?
On Mar 24, 2015 5:07 AM, "Tom Christie" notifications@github.com wrote:

Closed #2759
#2759.


Reply to this email directly or view it on GitHub
#2759 (comment)
.

@tomchristie
Copy link
Member

Sorry I closed this off as it was just the bare link.

The linked message seems like a reasonable conversation tho -
Could you pull the issue description into the ticket, instead of the bare link, and then we'll reopen and consider it.

Thanks! :)

@ar45
Copy link
Author

ar45 commented Mar 24, 2015

Done, Thanks.

@tomchristie
Copy link
Member

I guess in this case we need to return None.
We've request the field to use URLs, but the item hasn't been saved yet, and isn't yet associated with a cocrete, linkable file upload.
Does that sound like and acceptable behavior as far as you're concerned?

Not that if you were to call s.save() you wouldn't see this issue occuring, but this seems valid all the same.

@tomchristie tomchristie changed the title Not all File Objects have a .url attribute Unsaved File Objects may not have a .url attribute. Mar 24, 2015
@tomchristie
Copy link
Member

Update the title slightly to be a little more explicit.

@ar45
Copy link
Author

ar45 commented Mar 24, 2015

Sounds fair.

@@ -1106,7 +1106,7 @@ def to_internal_value(self, data):
return data

def to_representation(self, value):
if self.use_url:
if self.use_url and hasattr(value, 'url'):
if not value:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this if not value check should be moved to the top of the method instead of being hidden away here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True.

@kevin-brown
Copy link
Member

Looks like we have a general bug sitting around, and that's also causing a test failure here.

@xordoquy xordoquy added this to the 3.2.0 Release milestone Aug 1, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants