jonasundderwolf / django-image-cropping

Django helper application to easily and non-destructively crop arbitrarily large images in admin and frontend.
Other
553 stars 131 forks source link

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

Open huogerac opened 9 years ago

huogerac commented 9 years ago

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.

anrie commented 9 years ago

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:

huogerac commented 9 years ago

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

anrie commented 9 years ago

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.

T0SH1R0 commented 7 years ago

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?