django-image-cropping icon indicating copy to clipboard operation
django-image-cropping copied to clipboard

Make the ForeignKey field work outside the admin (forms/widgets)

Open huogerac opened this issue 10 years ago • 4 comments

Hi there,

Following the doc below (which has a note that it works just over the admin) https://github.com/jonasundderwolf/django-image-cropping#foreign-keys

and a model having a foreign key like this:

class NewsItem(models.Model): title = models.CharField(max_length=255) image = models.ForeignKey(Image) cropping = ImageRatioField('image__image_field', '120x100')

Should be great having it working outside the admin over custom forms. The ImageCropWidget and CropForeignKeyWidget are not working for this case.

If it is possible, should be perfect having directions for how to implement the foreignkey widget and what is missing, in this way any developer could help to fix it.

huogerac avatar Oct 08 '14 13:10 huogerac

Currently we are reusing the ForeignKeyRawIdWidget and at least in older versions of Django this widget is tied to the use in the admin. Apparently reusing the Widget in the Frontend got a lot easier nowadays.

So the task ahead would be to:

  • Check if the ForeignKeyRawIdWidget also works in ModelForms out of the box (when using Django > 1.4).
  • If it does, it would be good to enhance the example app for demonstration purposes and to adjust the docs accordingly. Otherwise it would be necessary to write a new widget.
  • Adjust the ImageCropField so it sets the correct widget and/or write a FormMixin for setting the appropriate widget.

anrie avatar Oct 13 '14 12:10 anrie

Hi @anrie , Thanks for answering and sorry my late reply.

You are right, the mentioned link works outside the Admin!

So, for the given model, we could use:

class NewsItemForm(ModelForm):
    class Meta:
        model = NewsItem
        widgets = {
            'image': ForeignKeyRawIdWidget(NewsItem._meta.get_field('image').rel, site),
        }

Alternatively and to avoid too much code (DRY) I've used the following code:


from django.contrib.admin.sites import site

from image_cropping.widgets import CropForeignKeyWidget

class CustomCropImageWidget(CropForeignKeyWidget):
    """ Image Crop Widget for ForeignKey fields 
        TODO: find out a way to auto discover the model and field_name params
    """
    def __init__(self, model, field_name):
        super(CustomCropImageWidget, self).__init__(model._meta.get_field(field_name).rel, site, field_name=field_name)

class NewsItemForm(ModelForm):
    class Meta:
        model = NewsItem
        widgets = {
            'image': CustomCropImageWidget(NewsItem, 'image'),
        }

However, I'm not totally happy by repeating the Model and the field_name as the Widget parameter, mainly because usually just setting a widget class is enough.

Anyway, I'd like to update the documentation, but first I'd like to check if the code is correct or exists some better approach.

I also wondering if we could add the CustomCropImageWidget over the django-image-cropping project.

Cheers

huogerac avatar Nov 22 '14 21:11 huogerac

Hey Roger,

adding a widget for ForeignKeys in ModelForms definitely makes sense so your contribution is very welcome. Right now we are quite busy, but I take a closer look at this issue once things settle a bit.

anrie avatar Nov 25 '14 09:11 anrie

No progress this far? That thing with ForeignKeyRawIdWidget isn't even in the docs yet. Don't u guys think that in 2017 with Django 1.11 people souldn't be forced to use django-admin?

T0SH1R0 avatar Jul 06 '17 14:07 T0SH1R0