system_user() 是在 django 中定义的吗?
我最初的问题实际上是如何将 User 外键添加到 Photolog 类型类(使用 Imagekit)
我看到 类似问题的答案,但是当我尝试实现它时,我得到全局名称'system_user'未定义
我对此并不感到惊讶,但令我惊讶的是,尽管它在答案中,但我在django中找不到对system_user
的引用文档。
(它不在 docs.djangoproject.com 上,Google for django+system_user 没有返回任何有趣的内容。)
我在 Photologue models.py 的 class Photo
中有这个,
def save(self, *args, **kwargs):
if self.title_slug is None:
self.title_slug = slugify(self.title)
if 'owner' not in self.__dict__:
self.owner = system_user() # this line fails
super(Photo, self).save(*args, **kwargs)
我应该如何导入 system_user()
,或者我可以在这里使用什么?
My original question was actually how to add a User foreign key to Photolog type class (that uses Imagekit)
I see an answer to a similar question, but when I tried to implement it, I get global name 'system_user' is not defined
I'm not surprised by that, but I am surprised that though it's in an answer, I can't find a reference to system_user
in django docs.
(It's not on docs.djangoproject.com, and Google for django+system_user returns nothing interesting.)
I have this in the class Photo
in Photologue models.py
def save(self, *args, **kwargs):
if self.title_slug is None:
self.title_slug = slugify(self.title)
if 'owner' not in self.__dict__:
self.owner = system_user() # this line fails
super(Photo, self).save(*args, **kwargs)
How should I import system_user()
, or what can I use here instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,
system_user
不是 django 函数。您应该将所有代码片段视为伪代码 - 他只是说“一个返回我的对象的函数”。grep -ri "system_user" /path/to/django 不返回任何内容,因此它不存在于 django 源代码中。
查看您链接到的问题中接受的答案,他重写了保存方法,传入用户对象,并手动将该对象关联到用户。
在您的情况下,由于您使用的是模型,因此必须将用户对象传递给模型
save()
方法。No,
system_user
is not a django function. You should take all code snippets as pseudo code -- he's just saying "a function that returns my object".grep -ri "system_user" /path/to/django
returns nothing, so it doesn't exist in the django source.Check out the accepted answer in the question you linked to, he overrides the save method, passes in the user object, and manually associates the object to the user.
In your case, since you're using a model, you'd have to pass in the user object to the model
save()
method.