我想將元素添加到我的數據庫(對于模型學生),同時將來自另一個模型(學校)的內容與學生的表單一起顯示。\這是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 的視圖(可能是因為沒有外鍵)。
添加回答
舉報
0/150
提交
取消