我正在编写一个导入脚本,以便在我的Django网站中将CSV格式化为模型。我希望这些创作出现在Django管理员的“历史”中,所以我写了这个函数,应该记录对象的创建。
from django.contrib.admin.models import LogEntry, ADDITION
def log_creation(created_object, message, uid=1):
#Doesn't work :(
LogEntry.objects.log_action(
user_id=uid,
content_type_id=get_content_type_for_model(created_object).pk,
object_id=created_object.pk,
object_repr=force_text(created_object),
action_flag=ADDITION,
change_message=message
)
我大部分都是在管理员中将其复制出this built in function。
但是,如果我使用新创建的对象作为输入运行它,我会收到此错误
/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Users/arya/PycharmProjects/PROJECTNAME/importers/plot-importer.py
Traceback (most recent call last):
File "/Users/arya/PycharmProjects/PROJECTNAME/importers/plot-importer.py", line 118, in <module>
import_plots(plot_csv)
File "/usr/local/lib/python2.7/site-packages/django/utils/decorators.py", line 145, in inner
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/reversion/revisions.py", line 297, in do_revision_context
return func(*args, **kwargs)
File "/Users/arya/PycharmProjects/PROJECTNAME/importers/plot-importer.py", line 106, in import_plots
log_creation(created_plot, message="Created plot through importer")
File "/Users/arya/PycharmProjects/PROJECTNAME/importers/plot-importer.py", line 26, in log_creation
change_message=message
File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/models.py", line 24, in log_action
e.save()
File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 710, in save
force_update=force_update, update_fields=update_fields)
File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 738, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 822, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 861, in _do_insert
using=using, raw=raw)
File "/usr/local/lib/python2.7/site-packages/django/db/models/manager.py", line 127, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 920, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 962, in execute_sql
for sql, params in self.as_sql():
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 920, in as_sql
for obj in self.query.objs
File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1940, in get_db_prep_save
return self.related_field.get_db_prep_save(value, connection=connection)
File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1886, in related_field
return self.foreign_related_fields[0]
File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1620, in foreign_related_fields
return tuple(rhs_field for lhs_field, rhs_field in self.related_fields)
File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1607, in related_fields
self._related_fields = self.resolve_related_fields()
File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1592, in resolve_related_fields
raise ValueError('Related model %r cannot be resolved' % self.rel.to)
ValueError: Related model 'auth.User' cannot be resolved
Process finished with exit code 1
知道如何避免这种情况吗?