我有另一个存储笔记的Django表单,并且它还自动存储用户,客户端和日期时间。我这次要做的是创建一些备注表单,允许您为特定客户添加备注,每次添加备注时,它都会显示最新结果,并且仅显示最新结果client_details.html
模板。因此,每次添加新笔记时,它都将继续替换现有笔记。我怎样才能做到这一点?
forms.py
class NoteForm(forms.ModelForm):
class Meta:
model = models.Note
exclude = ('client','user','datetime')
Views.py
@login_required
def get_client(request, client_id = 0):
client = None
try:
client = models.Client.objects.get(pk = client_id)
notes = client.note_set.all()
comments = client.comment_set.order_by('-datetime')
except:
pass
return render_to_response('client_details.html', {'client':client, 'notes':notes, 'comments':comments}, context_instance = RequestContext(request))
@login_required
def add_notes(request, client_id = 0):
client = None
contact = None
try:
client = models.Client.objects.get(pk = client_id)
except:
return HttpResponseNotFound()
if request.method == 'POST':
form = forms.NoteForm(request.POST)
if form.is_valid():
note = form.save(commit=False)
note.user = request.user
note.client = client
note.save(True)
request.user.message_set.create(message = "Note is successfully added.")
return HttpResponse("<script language=\"javascript\" type=\"text/javascript\">window.opener.location = window.opener.location; window.close();</script>")
else:
form = forms.NoteForm()
return render_to_response('note_form.html', {'form':form, 'client':client}, context_instance = RequestContext(request))
答案 0 :(得分:0)
添加到您的视图中:
try:
last_note = client.note_set.latest("datetime")
except DoesNotExist:
last_note = None