matthiask / django-imagefield

You should probably use this image field instead of Django's built-in models.ImageField.
https://django-imagefield.readthedocs.io
BSD 3-Clause "New" or "Revised" License
101 stars 10 forks source link

Use different widget #10

Open erwinelling opened 3 years ago

erwinelling commented 3 years ago

I am using ImageField on a model and use a Modelform to generate a form.

If I understand correctly, the form automatically uses the widget provided by this package, being ClearableFileInput, with and added preview of the image.

I'd like to use a different widget in my form, but overriding the widget on the form level, fails because it still seems to run this function:

def with_preview_and_ppoi(widget, **attrs):
    return type(
        str("%sWithPreviewAndPPOI" % widget.__name__),
        (PreviewAndPPOIMixin, widget),
        dict(attrs, __module__="imagefield.widgets"),
    )

I tried using just forms.ClearableFileInput, and get the error: 'ClearableFileInput' object has no attribute 'name'

I'm having difficulties wrapping my head around where to change or alter the widget. Would you be able to give me some pointers? If you think this hasn't got enough to do with this particular package, I would understand, but I'm not sure.

matthiask commented 3 years ago

Hi @erwinelling

I think the code in with_preview_and_ppoi assumes that widget is a widget class and not an instance; maybe you're passing forms.ClearableFileInput() and not forms.ClearableFileInput? That being said, ImageField.formfield already uses ClearableFileInput by default.

Maybe it works if you do not automatically generate the form field from the model? Is there a reason why you cannot use a default forms.ImageField in your form (if you do not want the preview and PPOI widget after all)