【问题标题】:Problem with saving a property of an object after validation验证后保存对象属性的问题
【发布时间】:2026-02-08 05:20:03
【问题描述】:

在我的 CreateView 类中,我创建了 Wholesale_Client 模型的一个实例。

在 form_valid() 函数中,我从表单中获取一些信息,我将在 create_woocommerce_client_individually() 函数中使用这些信息,以便在我的 woocommerce 网站上发布此记录。

woocommerce 网站中的每个用户都有一个帖子 ID,我将其作为 Wholesale_Client 模型中的属性。

我的目标是将批发客户实例存储在我的数据库和 woocommerce 网站中(成功),以获取当前存储的批发客户记录,以更新此用户的帖子 ID(从 null 到帖子 ID)。

获取记录后如何更新信息?

这是我的代码:

在网站中创建woocommerce客户端的功能

def create_woocommerce_client_individually(wcapi,name,surname,email):
        data = {

            "first_name": name,    
            "last_name": surname,
            "email": email,
            }
        wcapi.post("customers", data).json()

使用 api 将来自 woocommerce 网站的客户 ID 提供给我的记录的功能。

def give_cid_to_client_individually(wcapi,email):


    r=wcapi.get("customers?email="+str(email)).json()
    post_id=r[0]['id']

    fetched_client=Wholesale_Client.objects.get(email=email)
    fetched_client.cid=int(post_id)
    fetched_client.save()

这里(是问题所在)尽管记录已成功获取,但 post_id 并未保存到我的模型的 cid 属性中。

我的 CBV

class WholesaleClientCreateView(LoginRequiredMixin, CreateView):
    model = Wholesale_Client
    form_class = WholesaleClientForm
    success_url = reverse_lazy('wholesale_clients_list')
    template_name='wholesale_clients/wholesale_client_create_form.html'

def form_valid(self, form):
        print("Inside form valid...\n")

        if self.request.method == 'POST':
            form = WholesaleClientForm(self.request.POST)
            if form.is_valid():
                name = form.cleaned_data['name']
                surname = form.cleaned_data['surname']
                email = form.cleaned_data['email']
                wcapi=get_wcapi()
                create_woocommerce_client_individually(wcapi,name,surname,email)
                form.save()
                give_cid_to_client_individually(wcapi,email)
            else:
                form = WholesaleClientForm() 


        return super(WholesaleClientCreateView, self).form_valid(form)

【问题讨论】:

    标签: django forms validation


    【解决方案1】:

    我必须调用self.object 来引用我模型的当前对象,以便正确更新所需的属性(cid)。

    解决办法如下:

    def form_valid(self, form):
            print("Inside form valid...\n")
    
            if self.request.method == 'POST':
                form = WholesaleClientForm(self.request.POST)
                if form.is_valid():
    
                    self.object = form.save(commit=False)
                    name=self.object.name
                    #print("name : {} ".format(name))
                    surname=self.object.surname
                    #print("surname : {} ".format(name))
                    email=self.object.email
                    #print("email : {} \n".format(email))
                    wcapi=get_wcapi()
                    create_woocommerce_client_individually(wcapi,name,surname,email)
    
                    r=wcapi.get("customers?email="+str(email)).json()
                    post_id=r[0]['id']
    
                    self.object.cid=post_id
                    self.object.save()
    
                else:
                    form = WholesaleClientForm() 
    
    
            return super(WholesaleClientCreateView, self).form_valid(form)
    

    【讨论】: