【发布时间】:2017-12-03 13:19:09
【问题描述】:
我的timesheet.html 将包含ID、名称、startDate 和endDate 变量。我希望在 list_timesheet.html 的表格中显示所有这些字段。但我有一个问题,显示相同的 ID 和相同的名称多次不同的开始和结束日期。有谁知道我应该在我的 models.py 上做什么?现在我只能提交一次具有相同 ID 和名称的时间表,因为外键问题,当我尝试提交具有相同 ID 和名称但开始和结束日期不同的时间表时,它会显示错误提示“学生 ID和学生姓名已经存在”。
models.py
#consists of all the details in the timesheet
class Timesheet(models.Model):
studentID = models.CharField("Student ID", max_length=8, primary_key=True, default="")
studentName = models.CharField("Student Name", max_length=500, default="")
startDate = models.DateField("Start Date", max_length=8)
endDate = models.DateField("End Date", max_length=8)
def __str__(self):
return self.studentID
#consists of all the details of the timesheet under 'View Timesheets'
class LTimesheet(models.Model):
timesheet = models.OneToManyField(Timesheet, on_delete=models.CASCADE, primary_key=True)
status = models.CharField("Status", max_length=100)
【问题讨论】:
-
描述您的问题。很难理解您究竟需要什么帮助。
-
我认为您正在寻找
ForeignKey而不是OneToManyField。 stackoverflow.com/questions/6928692/… -
@HåkenLid 我已经描述了我的问题
-
@Brobin 之前尝试过 ForeignKey,但它不允许我以不同的开始和结束日期在相同的 ID 和名称上多次提交
标签: python django database models