strawberry-django icon indicating copy to clipboard operation
strawberry-django copied to clipboard

When the optional file field is used, an error will be reported when there are empty files in the query set

Open star2000 opened this issue 2 years ago • 4 comments

Error is thrown from here https://github.com/django/django/blob/475cffd1d64c690cdad16ede4d5e81985738ceb4/django/db/models/fields/files.py#L40

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

star2000 avatar Sep 18 '21 16:09 star2000

I avoid it like this

from django.db.models.fields import files


class FieldFile(files.FieldFile):
    def _require_file(self):
        pass


class FileField(files.FileField):
    attr_class = FieldFile


class ImageFieldFile(files.ImageFieldFile):
    def _require_file(self):
        pass


class ImageField(files.ImageField):
    attr_class = ImageFieldFile

star2000 avatar Sep 19 '21 12:09 star2000

Is there a more elegant way?

star2000 avatar Sep 19 '21 12:09 star2000

I avoid it like this

from django.db.models.fields import files


class FieldFile(files.FieldFile):
    def _require_file(self):
        pass


class FileField(files.FileField):
    attr_class = FieldFile


class ImageFieldFile(files.ImageFieldFile):
    def _require_file(self):
        pass


class ImageField(files.ImageField):
    attr_class = ImageFieldFile

A more elegant way:

from django.db.models.fields.files import FieldFile

FieldFile._require_file = lambda self: None  # type:ignore[attr-defined]

star2000 avatar Jan 03 '22 09:01 star2000

any fix for this? ) same issue: https://github.com/strawberry-graphql/strawberry-graphql-django/issues/22

mnogokotin avatar Dec 26 '22 10:12 mnogokotin