亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Django使用外鍵添加到數據庫,同時仍顯示來自其他模型的信息

Django使用外鍵添加到數據庫,同時仍顯示來自其他模型的信息

鴻蒙傳說 2021-08-11 21:30:20
我想將元素添加到我的數據庫(對于模型學生),同時將來自另一個模型(學校)的內容與學生的表單一起顯示。\這是models.pyclass School(models.Model):    name = models.CharField(max_length=256)    principal = models.CharField(max_length=256)    location = models.CharField(max_length=256)    def get_absolute_url(self):        return reverse('basiccbv:detail', kwargs={'pk':self.pk})    def __str__(self):        return self.nameclass Student(models.Model):    name = models.CharField(max_length=256)    age = models.PositiveIntegerField(validators= [validators.MinValueValidator(1),validators.MaxValueValidator(20)],default=1)    school = models.ForeignKey(School, related_name='students')def __str__(self):    return self.name在我的 views.py 我有這個:class SchoolDetailedView(DetailView):    context_object_name = 'school_detail'    model = models.School    template_name = 'basiccbv/school_detail.html'    # What i want is when I visit the link in the description I want to    # to see the school stuff and the form to add the student in this new     # viewclass StudentCreateView(CreateView):    model = models.School    # I tried using the Student but that I don't know how to display the     # school information, I tried with related_name = 'students' but it     # didn't work(I don't know if that can be done the way that intended it     # or I just don't have the knowledge )      fields = ['name', 'age']    # If I use School I could get the name of the school in the title and     # its primary key, but than I don't know how to display the form and     # vise versa     template_name = 'basiccbv/student_update.html'這是 .html 文件,可將我帶到需要表單的頁面。該鏈接是一個名為“basiccbv:studentupdate”的鏈接,這里使用了 related_name 學生,但我仍然不知道是否可以按照我想要的方式添加內容我真的被困住了,找不到任何關于這方面的信息,但如果你能幫助我或提供任何建議,謝謝。我過去添加學生的方式是使用 admin,對于學校,我使用 admin 直到我創建了用于創建沒有問題的 Schools 的視圖(可能是因為沒有外鍵)。
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 218 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號