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

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

Open
huogerac opened this issue Oct 8, 2014 · 4 comments
Open
Assignees

Comments

@huogerac
Copy link

huogerac commented Oct 8, 2014

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
Copy link
Member

anrie commented Oct 13, 2014

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.

@huogerac
Copy link
Author

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
Copy link
Member

anrie commented Nov 25, 2014

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 anrie self-assigned this Feb 26, 2015
@T0SH1R0
Copy link

T0SH1R0 commented Jul 6, 2017

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?

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

No branches or pull requests

3 participants