-
Notifications
You must be signed in to change notification settings - Fork 131
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
Comments
Currently we are reusing the So the task ahead would be to:
|
Hi @anrie , 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 |
Hey Roger, adding a widget for ForeignKeys in ModelForms definitely makes sense so your contribution is very welcome. |
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? |
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.
The text was updated successfully, but these errors were encountered: