我正在尝试更新int
模型中的Django
字段之一。
但是在更新此字段时出现以下错误。
(这是在ipython中使用python manage.py shell
)
# this is what I m trying to do
>> a = download.objects.get(id=1)
>> a.url = ""
>> a.save() # raises an error
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/usr/local/lib/python2.7/dist-packages/django/core/management/commands/shell.pyc in <module>()
----> 1 a.save()
/usr/local/lib/python2.7/dist-packages/django/db/models/base.pyc in save(self, force_insert, force_update, using, update_fields)
544
545 self.save_base(using=using, force_insert=force_insert,
--> 546 force_update=force_update, update_fields=update_fields)
547 save.alters_data = True
548
/usr/local/lib/python2.7/dist-packages/django/db/models/base.pyc in save_base(self, raw, cls, origin, force_insert, force_update, using, update_fields)
624 values = [(f, None, (raw and getattr(self, f.attname) or f.pre_save(self, False))) for f in non_pks]
625 if values:
--> 626 rows = manager.using(using).filter(pk=pk_val)._update(values)
627 if force_update and not rows:
628 raise DatabaseError("Forced update did not affect any rows.")
/usr/local/lib/python2.7/dist-packages/django/db/models/query.pyc in _update(self, values)
603 query.add_update_fields(values)
604 self._result_cache = None
--> 605 return query.get_compiler(self.db).execute_sql(None)
606 _update.alters_data = True
607
/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.pyc in execute_sql(self, result_type)
1012 related queries are not available.
1013 """
-> 1014 cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
1015 rows = cursor and cursor.rowcount or 0
1016 is_empty = cursor is None
/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.pyc in execute_sql(self, result_type)
828 """
829 try:
--> 830 sql, params = self.as_sql()
831 if not sql:
832 raise EmptyResultSet
/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.pyc in as_sql(self)
977 val = val.prepare_database_save(field)
978 else:
--> 979 val = field.get_db_prep_save(val, connection=self.connection)
980
981 # Getting the placeholder for the field.
/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.pyc in get_db_prep_save(self, value, connection)
302 """
303 return self.get_db_prep_value(value, connection=connection,
--> 304 prepared=False)
305
306 def get_prep_lookup(self, lookup_type, value):
/usr/local/lib/python2.7/dist-packages/jsonfield/fields.pyc in get_db_prep_value(self, value, connection, prepared)
47 if isinstance(value, basestring):
48 return value
---> 49 return json.dumps(value, **self.dump_kwargs)
50
51 def value_to_string(self, obj):
/usr/lib/python2.7/dist-packages/simplejson/__init__.pyc in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, encoding, default, use_decimal, namedtuple_as_object, tuple_as_array, **kw)
294 namedtuple_as_object=namedtuple_as_object,
295 tuple_as_array=tuple_as_array,
--> 296 **kw).encode(obj)
297
298
TypeError: __init__() got an unexpected keyword argument 'namedtuple_as_object'
有人可以建议我,这里出了什么问题?
答案 0 :(得分:6)
你在该模型中有一个json字段,你遇到了{em> simplejson 的问题documented in Django 1.5 release notes as a potential issue。
如果您通过pip uninstall simplejson
安装,则需要卸载 simplejson (pip
)。这样Django将使用Python的内置版本,它没有那些兼容性问题。