已知topic的id,,需要在entry新增中根據外來鍵topic建立新的entry,所以新的entry的id為外來鍵屬性(使用下劃線),topic_id=topic.id.
阿新 • • 發佈:2019-01-14
已知詳情頁的id,及回覆內容的content,如果建立新的記錄。這裡需要匹配到這條回覆對應的主題。一個主題詳情有很多個回覆,一個回覆只能對應一個主題。 ItemReply.objects.create(content=content, reply_item_id=id, reply_user_id=user.id) 非常重要 遺留問題,待解決。 因為,每一條主題,帶topic和user,這兩項是必須要選擇的,帶關聯性的表格如何輸入。如何新增。詳情頁要賦值topic和user 1、如果有主題和user則全部顯示出,供add時選擇即可 2、如果沒有則需要新增,新增後返回1,進行選擇,而後將選擇的值賦給這個記錄,用TodoItem.objects.create() 回覆後,需要在回覆的後面跟上回復,而不是直接續上, 總覺得detail方法和TodoItemReplyView.as_view()可以合併。 def post(self, request): """ 1 獲取輸入內容 2 賦值給ItemReply,存在2中情況:如下 1、 如果所有值都有,則建立資料記錄,設定引數,確定其是否可以遍歷。 在detail頁面迴圈遍歷顯示,包括內容,user,時間 返回 渲染到detail頁面 2、如果沒有,則重定向到detail,重新輸入。 返回 重定向 """ #接收資料 content = request.POST.get('reply',0) id = request.GET.get('id', 0) # user_name = request.GET.get('user_name',0) detail = TodoItem.objects.get(id=id) # get只能獲取一條,filter獲取的是查詢集,get獲取的是物件。 topic = detail.todoitem_topic user = detail.todoitem_user ItemReply.objects.create(content=content, reply_item_id=id, reply_user_id=user.id) reply_message = detail.itemreply_set.order_by('-date_added') # print(reply_message) # print(user) # print(detail) n = 1 context = { 'content': detail, 'user': user, 'topic': topic, 'n': n, 'id': id, 'reply_message': reply_message # 移到detail中,因為必須要一點開詳情就顯示回覆。 } # 處理資料 # 返回響應結果 # 獲取頁面id,在該id下進行回覆,是否可以合併到todo_detail中 # 回覆的回覆者以當前登入狀態下的使用人為準。 return render(request, 'todo_detail.html', context=context)