【发布时间】:2011-03-14 03:43:56
【问题描述】:
我看到similar to this has been asked before,但我想知道是否有更简单的方法来实现这一点。
还关注了this blog post。
下面给出了一个示例模型。
class Post (models.Model):
name = models.CharField(max_length=1000, help_text="required, name of the post")
description = models.TextField(blank=True)
created_datetime = models.DateTimeField(auto_now_add=True, editable=False)
modified_datetime = models.DateTimeField(auto_now=True, editable=False)
custom_hashed_url = models.CharField(unique=True, max_length=1000, editable=False)
def save(self, *args, **kwargs):
#How to save User here?
super(Model, self).save()
难道不能在调用 save() 之前将当前登录的用户发送到 Model 吗?
在视图中:
if request.method == 'POST':
if not errors:
f = PostForm(request.POST)
f.save()
【问题讨论】:
-
我一直在寻找解决这个问题的方法。我的用户已登录,我有外键设置,但它给了我一个错误
Cannot assign None: "MyModel.user" does not allow null values.
标签: django django-authentication