无法在没有主键的情况下强制更新 save() 。恐怖!
我不知道该怎么办,我是“空白”...
我有一个函数可以使用 request.GET 查询数据库并填写表单,您可以更改内容并保存更新信息。问题是 id 没有传递给函数,Django 选择 INSERT 而不是 UPDATE,代码如下:
看看这个混乱:
def meet_save(request):
if request.method == 'POST':
form = MeetingForm(request.POST)
if form.is_valid():
creator = get_object_or_404(Submitter, name=request.user)
meeting = Meeting(creator=creator)
meeting.ave = form.cleaned_data['ave']
meeting.st = form.cleaned_data['st']
meeting.date = form.cleaned_data['date']
#it is supposed to work with save()
#i tryed meeting.save(force_update=True)
#and i figured out the id is missing
meeting.save()
for people in form.cleaned_data['people']:
meeting.people.add(people)
return HttpResponseRedirect('/meeting/register/')
elif request.GET.has_key('meeting'):
id = request.GET['meeting']
people = ''
ave = ''
st = ''
date = ''
try:
meeting = meeting.objects.get(id=id)
#id = unicode(request.GET['meeting'])
#Have tryed different ways with no luck
id = request.GET['meeting']
ave = meeting.ave
st = meeting.st
date = meeting.date
except:
pass
form = MeetingForm({
'id':id, 'ave' : ave, 'st' : st,'date' : date
})
else:
form = MeetingForm()
variables = RequestContext(request, {
'form': form,
})
return render_to_response('meeting/meeting_save.html', variables)
它收到一个在 GET 请求中传递的 meet=id (例如/meeting/register/meeting=3) 并且表单获取数据,但保存时会创建一个新行而不是更新它,但它在 Django 中不会像这样工作,也许是 PHP 但不是 Django!
这是 Django 的正确代码:
def meet_save(request):
pk=request.GET['meeting'] #get the meeteng id from url
form = MeetingForm(request.POST)
if form.is_valid():
meeting, created = Meeting.objects.get_or_create(id=pk)
meeting.ave = form.cleaned_data['ave']
meeting.st = form.cleaned_data['st']
meeting.date = form.cleaned_data['date']
meeting.people = form.claned_data['people']
meeting.save()
#for people in form.cleaned_data['people']:
#meeting.people.add(people)
return HttpResponseRedirect('/meeting/register/')
else:
meeting = get_object_or_404(Meeting, pk=request.GET['meeting'])# this is the id passed
form = MeetingForm(instance=meeting)
variables = RequestContext(request, {
'form': form,
})
return render_to_response('meeting/meeting_save.html', variables)
我猜混乱
可能会起作用,但我修复了我的模板,因为POST 是这样的:method=POST action=".",当我到达该页面时,我发现该网址没问题,但是当在帖子中发送数据时,它会清除 meeting= id 我认为那是真实的恐怖!!我将其更改为 action="" 我在文档中没有看到任何有关它的内容,并且我读过它是相同的,请注意它不是。
姜戈摇滚!!!
i don't know what to do, i'm 'blank'...
I have a function that querys the database using a request.GET and fills the form, the you can change the contents and save updating the information. The problem is the id isn't passed to the function and Django opts for an INSERT instead of UPDATE, here's the code:
Look at this mess:
def meet_save(request):
if request.method == 'POST':
form = MeetingForm(request.POST)
if form.is_valid():
creator = get_object_or_404(Submitter, name=request.user)
meeting = Meeting(creator=creator)
meeting.ave = form.cleaned_data['ave']
meeting.st = form.cleaned_data['st']
meeting.date = form.cleaned_data['date']
#it is supposed to work with save()
#i tryed meeting.save(force_update=True)
#and i figured out the id is missing
meeting.save()
for people in form.cleaned_data['people']:
meeting.people.add(people)
return HttpResponseRedirect('/meeting/register/')
elif request.GET.has_key('meeting'):
id = request.GET['meeting']
people = ''
ave = ''
st = ''
date = ''
try:
meeting = meeting.objects.get(id=id)
#id = unicode(request.GET['meeting'])
#Have tryed different ways with no luck
id = request.GET['meeting']
ave = meeting.ave
st = meeting.st
date = meeting.date
except:
pass
form = MeetingForm({
'id':id, 'ave' : ave, 'st' : st,'date' : date
})
else:
form = MeetingForm()
variables = RequestContext(request, {
'form': form,
})
return render_to_response('meeting/meeting_save.html', variables)
It receives an meeting=id passed in GET request (like /meeting/register/meeting=3) and the form gets the data but when saving it creates a new row instead of updating it,but it doesn't wirk like this in Django, maybe PHP but not Django !!!
This is the right code for Django:
def meet_save(request):
pk=request.GET['meeting'] #get the meeteng id from url
form = MeetingForm(request.POST)
if form.is_valid():
meeting, created = Meeting.objects.get_or_create(id=pk)
meeting.ave = form.cleaned_data['ave']
meeting.st = form.cleaned_data['st']
meeting.date = form.cleaned_data['date']
meeting.people = form.claned_data['people']
meeting.save()
#for people in form.cleaned_data['people']:
#meeting.people.add(people)
return HttpResponseRedirect('/meeting/register/')
else:
meeting = get_object_or_404(Meeting, pk=request.GET['meeting'])# this is the id passed
form = MeetingForm(instance=meeting)
variables = RequestContext(request, {
'form': form,
})
return render_to_response('meeting/meeting_save.html', variables)
I guess the mess
maybe would work but i fixed my template because the POST was like this: method=POST action="." and i figured out the url was fine when i get to the page but when sending data in the post it clears out the meeting=id and i think that was the real Horror!! i changed it to action="" i have not seen anithing in the documentation about it and i have read it's the same, be aware it's not.
Django rocks !!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否有理由不使用 django 内置机制来使用关联对象填充表单?例如:
另外,您应该使用 django 快捷方式 get_object_or_404 ,以便可以避免不需要的行为。因此,它不会创建一个新对象,而只会发回 404。没有什么坏处。
Is there a reason why you aren't using the django built-in mechanism to fill the form with the associated object? For example:
Also, you should use the django shortcut get_object_or_404 so that you can evade unwanted behavior. So, instead of creating a new object, it will just send back a 404. No harm done.