我的模型结构如下:
@IBOutlet weak var name: UILabel!
@IBOutlet weak var city: UILabel!
func setRestaurtantData(restaurtant:hostelmodel)
{
self.name.text = restaurtant.name
self.city.text = restaurtant.city
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
我想遍历任务列表,如果实际日期字段不为空,即任务已完成,则将用户类中的tasks字段更新为1.我已编写以下代码:
class user(models.Model):
name = models.CharField(max_length=100)
tasks = models.IntegerField(default=0)
class project(models.Model):
worker = models.ForeignKey(user, on_delete=models.CASCADE)
project_name = models.CharField(max_length=100)
class task(models.Model):
project = models.ForeignKey(project, on_delete=models.CASCADE)
task_name = models.CharField(max_length=150)
expected_date = models.DateField(auto_now=False,auto_now_add=False,)
actual_date = models.DateField(auto_now=False,auto_now_add=False,blank=True,null=True,)
然而,这并没有给出理想的结果。我该怎么办?
答案 0 :(得分:1)
修改后没有保存对象 - 只需修改该值就不会将其写入数据库。试试这个:
a = task.objects.filter(actual_date__isnull=False)
for x in a:
worker = x.project.worker
worker.tasks += 1
worker.save()
另外,您应该考虑遵循PEP8约定并使用CamelCase作为您的类名。目前,您可以非常轻松地将类与对象混合在一起。