0
我建立了一個模板,其中填充了來自數據庫的數據並且用戶可以更新更改。問題是,我的代碼正在創建一條新記錄,而不是更新現有的記錄。Django創建新記錄而不是更新
型號:
class Users(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
email = models.CharField(max_length=100)
class Event(models.Model):
event_admin = models.ForeignKey(Users)
event_name = models.CharField(max_length=50)
event_location = models.CharField(max_length=50)
event_date = models.DateField()
event_time = models.TimeField()
event_notes = models.CharField(max_length=200)
查看:
def edit_event(request, id):
if request.method == 'POST':
e1 = CreateEventForm(request.POST)
if e1.is_valid():
e1 = e1.save(commit=False)
temp = get_object_or_404(Event, id = id)
admin = temp.event_admin
e1.event_admin = admin
e1.save()
eventID = e1.id
return HttpResponseRedirect('/app/' + str(eventID))
else:
event = CreateEventForm(instance=get_object_or_404(Event, id=id))
return render(request, 'edit_event.html', {'event':event})
我相信我的問題是,我不是更新當前對象,而是建立一個新的。但是,在處理現有記錄時,我遇到了分配event_admin中所需外鍵的問題。