自定义表单作者 - 自动将作者保存到数据库
我有一个自定义表单...我想自动保存表单数据的作者(经过身份验证的用户)。我正在使用 ModelForm 来创建表单。
models.py
class Tracker(models.Model):
client = models.ForeignKey(Clients)
user = models.ForeignKey(User)
description = models.TextField()
...
我还将自定义配置文件链接到 django users 表...身份验证也工作正常...我可以读取用户和 id...
forms.py
class TrackerForm(ModelForm):
def __init__(self, *args, **kwargs):
super(TrackerForm, self).__init__(*args, **kwargs)
self.fields.keyOrder = ['date_job_start','date_job_end','description','onsite','billable','client']
class Meta:
model = Tracker
widgets = {
'client': HiddenInput(),
}
当从上层创建表单时,我尝试保存它...它需要用户数据(缺少警告)。我怎样才能改变它,以便它自动保存经过身份验证的用户而不是询问它?我知道我没有在这里定义用户字段...那是因为我不想要它的下拉菜单...我希望从身份验证中保存用户...没有任何选择或显示...
PS:我知道 initial
选项...一定有更好的方法吗?
谢谢! BR
I have a custom form... I'd like to auto save the author (authenticated user) for the form data. I'm using ModelForm for creating the form.
models.py
class Tracker(models.Model):
client = models.ForeignKey(Clients)
user = models.ForeignKey(User)
description = models.TextField()
...
I have also linked the custom profile to the django users table... also the auth works fine... I can read the user and id...
forms.py
class TrackerForm(ModelForm):
def __init__(self, *args, **kwargs):
super(TrackerForm, self).__init__(*args, **kwargs)
self.fields.keyOrder = ['date_job_start','date_job_end','description','onsite','billable','client']
class Meta:
model = Tracker
widgets = {
'client': HiddenInput(),
}
When the form is created from the upper class and I try to save it... it wants the user data (missing warning). How can I change it so that it would automatically save the authenticated user instead of asking for it?? I know that I dont' have the user field defined here... that's because I don't want a dropdown for it... I want the user to be saved from the auth...without any selection or display...
P.S.: I know about the initial
option... there must be a better way?
Thanks!
BR
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
并认为:
And in view: