Django form.save 一步一步

发布于 2024-09-07 08:55:50 字数 1428 浏览 1 评论 0原文

假设我有一个用于添加/编辑产品的表单(其中字段“用户”是我的用户的外键),由两个单独的视图功能触发 - 添加/编辑:

def product_add(request):
    userprofile = UserProfile.objects.get(user=request.user)
    if request.method == 'POST':
        form = ProductAddForm(request.POST, request.FILES,)
        if form.is_valid():
            form.save(user=request.user)
    else:
        form = ProductAddForm()
    return render_to_response('products/product_add.html', {
            'form':form, 'user':request.user,
            }, context_instance=RequestContext(request))

def product_edit(request, id):
    product = get_object_or_404(Product, id=id, user=request.user)
    if product.user.id!=request.user.id:
        raise Http404
    if request.method == 'POST':
        form = ProductAddForm(request.POST, request.FILES, instance=product)
        if form.is_valid():
            form.save(user=request.user)            
    else:
        form = ProductAddForm(instance=product)
    return render_to_response('products/product_edit.html', {
            'form':form, 'user':request.user,
            }, context_instance=RequestContext(request))

表单的保存方法如下所示:

def save(self, user, *args, **kwargs):
    self.instance.user = user                           
    post = super(ProductAddForm, self).save(*args, **kwargs)
    post.save()

有人可以告诉我发生了什么吗在这个保存方法中一步一步?为什么我们在这个表单上调用super?如果函数调用相同,那么我们编辑和保存新产品时整个处理有什么不同?

Let's say I have a form for adding/editing products (with field 'user' being a foreign key to my User) triggered from two separate view functions - add/edit :

def product_add(request):
    userprofile = UserProfile.objects.get(user=request.user)
    if request.method == 'POST':
        form = ProductAddForm(request.POST, request.FILES,)
        if form.is_valid():
            form.save(user=request.user)
    else:
        form = ProductAddForm()
    return render_to_response('products/product_add.html', {
            'form':form, 'user':request.user,
            }, context_instance=RequestContext(request))

def product_edit(request, id):
    product = get_object_or_404(Product, id=id, user=request.user)
    if product.user.id!=request.user.id:
        raise Http404
    if request.method == 'POST':
        form = ProductAddForm(request.POST, request.FILES, instance=product)
        if form.is_valid():
            form.save(user=request.user)            
    else:
        form = ProductAddForm(instance=product)
    return render_to_response('products/product_edit.html', {
            'form':form, 'user':request.user,
            }, context_instance=RequestContext(request))

The form's save method looks as follows :

def save(self, user, *args, **kwargs):
    self.instance.user = user                           
    post = super(ProductAddForm, self).save(*args, **kwargs)
    post.save()

Can somebody tell me what's happening in this save method step by step ?Why do we call super on this form and what is the difference in whole processing when we edit and save new product if function call is the same ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

傾旎 2024-09-14 08:55:50

self.instance.user=用户

将 user 参数保存到 self.instance 对象的 user 属性中

post = super(ProductAddForm, self).save(*args, **kwargs)

调用超类 save 方法以获取结果对象。

post.save()

保存结果对象。这可能是不必要的,也可能是必要的,具体取决于 save 的参数。

请参阅 http://docs.djangoproject.com/ en/1.2/topics/forms/modelforms/#the-save-method。如果 commit 为 False,则 post 对象尚未保存到数据库中。

self.instance.user = user

Save the user argument into the self.instance object's user attribute

post = super(ProductAddForm, self).save(*args, **kwargs)

Invoke the superclass save method to get a resulting object.

post.save()

Save the resulting object. This may be needless, or it may be essential, depending on the arguments to save.

See http://docs.djangoproject.com/en/1.2/topics/forms/modelforms/#the-save-method. If commit is False, then the post object has not been saved to the database.

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