Django form.save 一步一步
假设我有一个用于添加/编辑产品的表单(其中字段“用户”是我的用户的外键),由两个单独的视图功能触发 - 添加/编辑:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 user 参数保存到 self.instance 对象的 user 属性中
调用超类 save 方法以获取结果对象。
保存结果对象。这可能是不必要的,也可能是必要的,具体取决于
save
的参数。请参阅 http://docs.djangoproject.com/ en/1.2/topics/forms/modelforms/#the-save-method。如果 commit 为 False,则
post
对象尚未保存到数据库中。Save the user argument into the self.instance object's user attribute
Invoke the superclass save method to get a resulting object.
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.