Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 3 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
Showing with 27 additions and 17 deletions.
  1. +12 −8 README.rst
  2. +3 −3 example/models.py
  3. +3 −2 image_cropping/admin.py
  4. +9 −4 image_cropping/fields.py
View
20 README.rst
@@ -95,29 +95,33 @@ If you need the same image in multiple formats, simply specify another ImageRati
# size is "width x height"
list_page_cropping = ImageRatioField('image', '200x100')
detail_page_cropping = ImageRatioField('image', '430x360')
-
+
In your templates, use the corresponding ratio field::
{% thumbnail yourmodel.image 200x100 box=yourmodel.list_page_cropping crop detail %}
-If you need to crop an image contained within another model, referenced by a ForeignKey, use a ``CropForeignKey``. This
-field works like a regular ForeignKey but expects a keyword argument named ``field_name``. The model attribute with this
-name needs to be an ImageField that will be cropped::
+If you need to crop an image contained within another model, referenced by a ForeignKey, use a ``ForeignKey``. The fieldname
+in the ``ImageRadioField`` is now composed of the ``ForeignKey`` name, double underscore and the ``ImageField`` name::
from django.db import models
from image_cropping.fields import ImageRatioField, CropForeignKey
-
+
class Image(models.Model):
image_field = models.ImageField(upload_to='image/')
class NewsItem(models.Model):
title = models.CharField(max_length=255)
- image = CropForeignKey(Image, 'image_field')
- cropping = ImageRatioField('image', '120x100')
+ image = ForeignKey(Image)
+ cropping = ImageRatioField('image__image_field', '120x100')
-The CropForeignKey works only in the admin for now, as it uses the ``raw_id`` widget.
+The ForeignKey works only in the admin for now, as it uses the ``raw_id`` widget.
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.
View
6 example/models.py
@@ -1,5 +1,5 @@
from django.db import models
-from image_cropping.fields import ImageRatioField, ImageCropField, CropForeignKey
+from image_cropping.fields import ImageRatioField, ImageCropField
class Image(models.Model):
@@ -11,8 +11,8 @@ class Meta:
class ImageFK(models.Model):
- image = CropForeignKey(Image, 'image_field')
- cropping = ImageRatioField('image', '120x100')
+ image = models.ForeignKey(Image)
+ cropping = ImageRatioField('image__image_field', '120x100')
class Meta:
app_label = 'example'
View
5 image_cropping/admin.py
@@ -6,7 +6,8 @@ class ImageCroppingAdmin(object):
def formfield_for_dbfield(self, db_field, **kwargs):
formfield = super(ImageCroppingAdmin, self).formfield_for_dbfield(db_field, **kwargs)
- if hasattr(db_field, 'related') and db_field.name in self.model.crop_fk_fields:
- formfield.widget = CropForeignKeyWidget(db_field.rel, field_name=db_field.field_name,
+ if hasattr(db_field, 'related') and db_field.name in self.model.crop_fk_fields.keys():
+ formfield.widget = CropForeignKeyWidget(db_field.rel,
+ field_name=self.model.crop_fk_fields[db_field.name],
admin_site=self.admin_site)
return formfield
View
13 image_cropping/fields.py
@@ -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
@@ -44,8 +44,13 @@ def south_field_triple(self):
class ImageRatioField(models.CharField):
def __init__(self, image_field, size, adapt_rotation=False, verbose_name=None,
size_warning=getattr(settings, 'IMAGE_CROPPING_SIZE_WARNING', False)):
+ try:
+ self.image_field, self.image_fkfield = image_field.split('__', 1)
+ except ValueError:
+ self.image_field = image_field
+ self.image_fkfield = ''
+
self.width, self.height = size.split('x')
- self.image_field = image_field
self.adapt_rotation = adapt_rotation
self.size_warning = size_warning
super(ImageRatioField, self).__init__(max_length=255, blank=True, verbose_name=verbose_name)
@@ -55,8 +60,8 @@ def contribute_to_class(self, 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)
+ cls.add_to_class('crop_fk_fields', {})
+ cls.crop_fk_fields[self.image_field] = self.image_fkfield
def formfield(self, *args, **kwargs):
kwargs['widget'] = forms.TextInput(attrs={

No commit comments for this range

Something went wrong with that request. Please try again.