Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 猴子修补django模型场_Python_Django_Monkeypatching - Fatal编程技术网

Python 猴子修补django模型场

Python 猴子修补django模型场,python,django,monkeypatching,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, siz

我正在尝试修补并替换模型中的一个字段

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'

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

啊,太酷了,我设法找到了相应的
contribution\u to\u class
,看看那里发生了什么神奇的事情。我不应该做
setattr(ProductAttributeValue,fieldname\u to\u replace,replacement\u field)
我应该做
setattr(ProductAttributeValue,fieldname\u to\u replace,replacement\u field.description\u class(replacement\u field))