Permalink
Browse files

Deprecate CropForeignKeyField

... we rather use a standard ForeignKey and let the ImageCroppingAdmin
select the widget
  • Loading branch information...
1 parent 5a07ce7 commit 5c4ec6833f20899ef23078056a8c5ade63870eda @anrie anrie committed Mar 12, 2012
Showing with 22 additions and 6 deletions.
  1. +8 −1 README.rst
  2. +5 −4 image_cropping/admin.py
  3. +9 −1 image_cropping/fields.py
View
@@ -118,4 +118,11 @@ name needs to be an ImageField that will be cropped::
cropping = ImageRatioField('image', '120x100')
The CropForeignKey works only in the admin for now, as it uses the ``raw_id`` widget.
-To enable the widget your ModelAdmin has to inherit from ``ImageCroppingAdmin``.
+
+To enable the widget the ModelAdmin containing your field has to inherit from ``ImageCroppingAdmin``.
+
+Deprecation Warning
+-------------------
+
+In future versions of ``django-image-cropping`` the ``CropForeignKey`` will be removed.
+You can now simply use a regular ``ForeignKey`` instead.
@@ -1,13 +1,14 @@
+from django import VERSION
from django.contrib import admin
from .widgets import CropForeignKeyWidget
-from .fields import CropForeignKey
class ImageCroppingAdmin(admin.ModelAdmin):
def formfield_for_dbfield(self, db_field, **kwargs):
formfield = super(ImageCroppingAdmin, self).formfield_for_dbfield(db_field, **kwargs)
- if isinstance(db_field, CropForeignKey):
- formfield.widget = CropForeignKeyWidget(db_field.rel, field_name=db_field.name, using=kwargs.get('using'))
+
+ if hasattr(db_field, 'related') and db_field.name in self.model.crop_fk_fields:
#Django 1.4: pass a reference to the admin_site which is nowadays needed by ForeignKeyRawIdWidget
- #formfield.widget = CropForeignKeyWidget(db_field.rel, field_name=db_field.name, using=kwargs.get('using'), admin_site=self.admin_site)
+ kwargs = {'admin_site': self.admin_site} if VERSION >= (1, 4, 0) else {}
+ formfield.widget = CropForeignKeyWidget(db_field.rel, field_name=db_field.name, **kwargs)
return formfield
@@ -19,7 +19,7 @@ def south_field_triple(self):
args, kwargs = introspector(self)
return (field_class, args, kwargs)
-
+#deprecated, as we now set the widget in the ModelAdmin
class CropForeignKey(models.ForeignKey):
'''
A croppable image field contained in another model. Only works in admin
@@ -50,6 +50,14 @@ def __init__(self, image_field, size, adapt_rotation=False, verbose_name=None,
self.size_warning = size_warning
super(ImageRatioField, self).__init__(max_length=255, blank=True, verbose_name=verbose_name)
+ def contribute_to_class(self, cls, name):
+ super(ImageRatioField, self).contribute_to_class(cls, name)
+ # attach a list of fields that are referenced by the ImageRatioField
+ # so we can set the correct widget in the ModelAdmin
+ if not hasattr(cls, 'crop_fk_fields'):
+ cls.add_to_class('crop_fk_fields', [])
+ cls.crop_fk_fields.append(self.image_field)
+
def formfield(self, *args, **kwargs):
kwargs['widget'] = forms.TextInput(attrs={
'data-width': int(self.width),

0 comments on commit 5c4ec68

Please sign in to comment.