猴子修补django模型领域

时间:2014-01-07 00:49:36

标签: python django monkeypatching

我正试图修补补丁并替换我模型中的一个字段。

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 """

要替换的字段是ImageField。不知怎的,当实际的保存开始时,我无法让它工作。

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'

在修补过程中我错过了什么吗?

1 个答案:

答案 0 :(得分:1)

很酷,我找到了相应的contribute_to_class,看看那里发生了什么魔法。而不是setattr(ProductAttributeValue, fieldname_to_replace, replacement_field)我应该做setattr(ProductAttributeValue, fieldname_to_replace, replacement_field.description_class(replacement_field))而不是。{/ p>