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

ImageFieldFile causes AttributeError exception to be thrown. #201

Open
DelboyJay opened this issue Jun 28, 2022 · 2 comments
Open

ImageFieldFile causes AttributeError exception to be thrown. #201

DelboyJay opened this issue Jun 28, 2022 · 2 comments
Labels

Comments

@DelboyJay
Copy link

Describe the bug
When the model has one or more django.db.models.fields.files.ImageFieldFile fields and the _as_dict function is called an {AttributeError} 'NoneType' object has no attribute 'seek' is raised when the deepcopy line is executed.

I found a solution to the problem was to insert a check for the ImageFieldFile class in the _as_dict function and cast it to a string that returns the filename as shown below.

def _as_dict(self, check_relationship, include_primary_key=True):
...
   if isinstance(field_value, ImageFieldFile):
       # deepcopy of ImageFieldFile causes an AttributeError 'NoneType' object has no attribute 'seek'
       field_value = str(field_value)

   # Explanation of copy usage here :
   # https://github.com/romgar/django-dirtyfields/commit/efd0286db8b874b5d6bd06c9e903b1a0c9cc6b00
   all_field[field.name] = deepcopy(field_value)
...

As a temporary workaround, I derived a new class from DirtyFieldsMixin, copied the entire _as_dict function, and added in the code above. This is not great though as I'd prefer to call your existing code in case it changes in the future.

Environment (please complete the following information):

  • OS: Ubuntu/Zorin 16
  • Python version: 3.9.13
  • Django version: problem exists for 3.1.14 & 3.2.13
  • django-dirtyfields version: 1.8.1
@DelboyJay DelboyJay added the Bug label Jun 28, 2022
@Alamgir-K
Copy link

Alamgir-K commented Jul 4, 2022

Tried to replicate the bug but was not able to (even with the same environment versions). Please see the code below for some of the testing I did. The test passed even without the IF check you outlined. Perhaps it would be of help if you're able to provide your usage of the ImageField to see where the issue is.

class ImageFieldModel(DirtyFieldsMixin, models.Model):
    image1 = models.ImageField(upload_to="image1/")
@pytest.mark.django_db
def test_file_fields_real_images():
    tm = ImageFieldModel()
    # field is dirty because model is unsaved
    assert tm.get_dirty_fields() == {"image1": FakeFieldFile("")}
    tm.save()
    assert tm.get_dirty_fields() == {}

    # set file makes field dirty
    with open(join(dirname(__file__), "files", "foo.jpg"), "rb") as f:
        tm.image1.save("test-file-3.jpg", ImageFile(f), save=False)
    assert tm.get_dirty_fields() == {"image1": FakeFieldFile("")}
    tm.save()
    assert tm.get_dirty_fields() == {}

    # change file makes field dirty
    with open(join(dirname(__file__), "files", "bar.jpg"), "rb") as f:
        tm.image1.save("test-file-4.jpg", ImageFile(f), save=False)
    assert tm.get_dirty_fields() == {"image1": FakeFieldFile("image1/test-file-3.jpg")}
    tm.save()
    assert tm.get_dirty_fields() == {}

@LincolnPuzey
Copy link
Collaborator

@DelboyJay if you don't share your code that is causing the AttributeError we can only speculate on what is happening.

I have also just merged #203 which is a similar patch to yours, so might fix your issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants