我正在尝试更新一个对象,但我得到:“主键必须是唯一的”...... Entry 的模型:
class Entry(models.Model):
title = models.CharField(max_length=250)
author = models.ForeignKey(User, editable=False)
status = models.IntegerField(choices=data_types.STATUS_CHOICES,
default = data_types.STATUS_DRAFT)
和观点:
@login_required
def edit_entry(request, entry_id='0'):
message = ""
entry = get_object_or_404(Entry.objects, pk=entry_id)
if request.method == 'GET':
form = EntryForm(instance=entry)
else :
if request.method == 'POST':
if request.POST['submit'] == 'Edit':
print "entry id: %s" % entry.id
form = EntryForm(request.POST, instance=entry)
if form.is_valid():
secondEntry = form.save(False)
print "second entry id: %s" % secondEntry.id
form.save()
message = "entry updated"
else:
message = 'There were errors'
return render_to_response(
'myadmin/edit_entry.html',
{ 'entryForm':form,'message': message},
context_instance=RequestContext(request))
因此,我打印了两个 ID,它们是相同的: 1. 这应该使 form.save() 更新对象,但相反,它会尝试插入它……知道吗?