我有一个时间表模型,如下所示:-
class time_table(models.Model):
username = models.ForeignKey(User,db_column="username", on_delete=models.CASCADE,)
sem_section = models.ForeignKey(sem_sections, db_column = "sem_section",on_delete=models.CASCADE,)
subject_id = models.ForeignKey(subjects,db_column="subject_id", on_delete=models.CASCADE,)
day_name = models.ForeignKey(days_ref, db_column = "days_ref",on_delete=models.CASCADE,)
hour_no = models.ForeignKey(hours, db_column = "hour_no",on_delete=models.CASCADE,)
def __str__(self):
ret = str(self.username) +' takes ' + str(self.sem_section) + " class " + str(self.subject_id) + " on " + str(self.hour_no) + " on " + str(self.day_name);
return ret
class Meta:
constraints = [
models.UniqueConstraint(fields=['sem_section', 'day_name', 'hour_no'], name='Allotment_check')
]
我必须为插入的每个元组创建多个记录,例如,如果我在第4天的星期一插入某个主题的插槽,则必须为整个一年或一个学期的多个重复日期创建它。关于如何调整代码的任何建议或为实现此目标而添加的任何其他代码。我正在使用Postgres数据库。
您可以将Django的bulk_create与(python)列表理解一起使用来创建多个对象。