django 如何在UpdateView中获取当前对象?

nc1teljy  于 2022-11-19  发布在  Go
关注(0)|答案(1)|浏览(124)

为了更新表中的数据,我使用了从UpdateView继承的类o,这些字段是从数据库自动填充的,但是我能在我的类中以某种方式从user_guid字段中获取数据吗?
下面是我的窗体和类代码:
第一个

yduiuuwa

yduiuuwa1#

您可以使用self.object.user_guid来获取user_guid字段的数据,因此:

class CampaignEditor(UpdateView):
    model = Campaigns
    template_name = 'mailsinfo/add_campaign.html'
    form_class = CampaignsForm

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        data = list(Emails.objects.values()) # you may want to further filter for update purposes
        data = MailsTableWithoutPass(data)
        context['data'] = data
        context['user_guid_field'] = self.object.user_guid
        return context

相关问题