Question

I'm trying to monkey patch and replace one of the fields in my model.

class ProductAttributeValue(AbstractProductAttributeValue):
    pass

""" monkey patching value_image field start """

fieldname_to_replace = 'value_image'

replacement_field = m2.ImageWithThumbsField(upload_to = get_file_path, sizes = ((100, 100),), blank=True, null=True)
replacement_field.attname = fieldname_to_replace
setattr(ProductAttributeValue, fieldname_to_replace, replacement_field)

for i, field in enumerate(ProductAttributeValue._meta.fields):
    if field.attname == fieldname_to_replace:
        ProductAttributeValue._meta.fields[i] = replacement_field

""" monkey patching value_image field end """

The field to be replaced is a ImageField. Somehow I couldn't get it to work when the actual saving begins.

Traceback (most recent call last):
  File "/Users/carrier24sg/.virtualenvs/pastacard/lib/python2.7/site-packages/django/core/handlers/base.py", line 115, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/Users/carrier24sg/.virtualenvs/pastacard/lib/python2.7/site-packages/django/utils/decorators.py", line 91, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/Users/carrier24sg/.virtualenvs/pastacard/lib/python2.7/site-packages/django/views/decorators/cache.py", line 89, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "/Users/carrier24sg/.virtualenvs/pastacard/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 202, in inner
    return view(request, *args, **kwargs)
  File "/Users/carrier24sg/.virtualenvs/pastacard/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/carrier24sg/.virtualenvs/pastacard/lib/python2.7/site-packages/django/views/generic/base.py", line 86, in dispatch
    return handler(request, *args, **kwargs)
  File "/Users/carrier24sg/.virtualenvs/pastacard/lib/python2.7/site-packages/django/views/generic/edit.py", line 222, in post
    return super(BaseUpdateView, self).post(request, *args, **kwargs)
  File "/Users/carrier24sg/.virtualenvs/pastacard/lib/python2.7/site-packages/django/views/generic/edit.py", line 165, in post
    return self.form_valid(form)
  File "/Users/carrier24sg/workspace/pastacard/pastacard/pastacard/admin_views.py", line 70, in form_valid
    return self.process_all_forms(form)
  File "/Users/carrier24sg/workspace/pastacard/pastacard/pastacard/admin_views.py", line 83, in process_all_forms
    self.object = form.save()
  File "/Users/carrier24sg/.virtualenvs/pastacard/lib/python2.7/site-packages/oscar/apps/dashboard/catalogue/forms.py", line 303, in save
    object.save()
  File "/Users/carrier24sg/.virtualenvs/pastacard/lib/python2.7/site-packages/oscar/apps/catalogue/abstract_models.py", line 380, in save
    self.attr.save()
  File "/Users/carrier24sg/.virtualenvs/pastacard/lib/python2.7/site-packages/oscar/apps/catalogue/abstract_models.py", line 701, in save
    attribute.save_value(self.product, value)
  File "/Users/carrier24sg/.virtualenvs/pastacard/lib/python2.7/site-packages/oscar/apps/catalogue/abstract_models.py", line 855, in save_value
    value_obj.save()
  File "/Users/carrier24sg/.virtualenvs/pastacard/lib/python2.7/site-packages/django/db/models/base.py", line 546, in save
    force_update=force_update, update_fields=update_fields)
  File "/Users/carrier24sg/.virtualenvs/pastacard/lib/python2.7/site-packages/django/db/models/base.py", line 624, in save_base
    values = [(f, None, (raw and getattr(self, f.attname) or f.pre_save(self, False))) for f in non_pks]
  File "/Users/carrier24sg/.virtualenvs/pastacard/lib/python2.7/site-packages/django/db/models/fields/files.py", line 248, in pre_save
    if file and not file._committed:
AttributeError: 'InMemoryUploadedFile' object has no attribute '_committed'

Did I miss out something during the patching?

No correct solution

OTHER TIPS

Ah cool, I managed to find the corresponding contribute_to_class and see what magic is happening there. Instead of doing setattr(ProductAttributeValue, fieldname_to_replace, replacement_field) I should be doing setattr(ProductAttributeValue, fieldname_to_replace, replacement_field.description_class(replacement_field)) instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top