2
我想猴子修補程序並替換我的模型中的字段之一。猴子修補django模型字段
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'
我在補丁期間錯過了什麼嗎?