自定义表单作者 - 自动将作者保存到数据库

发布于 2024-11-15 09:46:10 字数 932 浏览 5 评论 0原文

我有一个自定义表单...我想自动保存表单数据的作者(经过身份验证的用户)。我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲影 2024-11-22 09:46:10
class TrackerForm(ModelForm):
    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')
        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(),
        }
        exclude = ('user',)

    def save(self):
        obj = super(TrackerForm, self).save(commit=False)
        obj.user = self.user
        obj.save()
        return obj

并认为:

form = TrackerForm(user=request.user) #and other parameters
class TrackerForm(ModelForm):
    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')
        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(),
        }
        exclude = ('user',)

    def save(self):
        obj = super(TrackerForm, self).save(commit=False)
        obj.user = self.user
        obj.save()
        return obj

And in view:

form = TrackerForm(user=request.user) #and other parameters
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文