转换日期不作为日期更新 [英] Shift Date not updated as Sit date

查看:198
本文介绍了转换日期不作为日期更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计了两种型号:一种用于存储起飞日期的模型和用于存储换档日期的另一种模式。两个模型通过外键链接。这是我的两个模型:

  class Sitting(models.Model):
sit_date = models.DateField(blank = False,unique = True)
cut_off_date = models.DateField(null = True,blank = True)
ballot_date = models.DateField(null = True,blank = True)
sess_no = models。 ForeignKey(Session,
on_delete = models.CASCADE)
genre = TreeForeignKey('Genre',null = True,blank = True,db_index = True)
/ pre>

Shift模型:

  class Shiftdate(models.Model )
shift_date = models.DateField(blank = False,unique = True)
sit_date = models.ForeignKey(Sitting,
on_delete = models.CASCADE)

使用Shift模型将现有的休息日期更改为其他日期,如下所示:

 坐位日期变动日期
2016-09-01 2016年9月15日
2016-09-08 2016年9月19日

现在我想把换班日期变成一个新的休息日。为此,我在Sitting模型中定义以下方法:

  def get_shift_date(self):
return self.sit_date.shift_date

这个shift_date要保存在Sitting模型中。我定义以下保存方法:

  def save(self,* args,** kwargs):

self.sit_date = self.get_shift_date()
super(Sitting,self).save(* args,** kwargs)

但是,这不会作为会议日期返回班次。有人可以帮我修理这段代码吗?

解决方案

如果 sit_date 为空,则您的保存方法仅调用 get_shift_date 但是在该方法中,您只返回一些 sit_date 空白。所以sit_date只能设置为无。


I designed two models: one model for storing Sitting date and another mode for storing Shifting date. Two model are linked by foreign key. Here is my two models:

class Sitting(models.Model):
    sit_date = models.DateField(blank=False,unique=True)
    cut_off_date = models.DateField(null=True, blank=True)
    ballot_date = models.DateField(null=True, blank=True)
    sess_no = models.ForeignKey(Session,
                                 on_delete=models.CASCADE)
    genre = TreeForeignKey('Genre', null=True, blank=True, db_index=True)

Shift Model:

 class Shiftdate(models.Model):
    shift_date = models.DateField(blank=False,unique=True)
    sit_date = models.ForeignKey(Sitting,
                                on_delete=models.CASCADE)

Using Shift model I change existing sitting dates to other dates as:

Sitting Date     Shifting Date
2016-09-01       Sept. 15, 2016
2016-09-08       Sept. 19, 2016

Now I want to make shift date to a new sitting dates. For this I define following method in Sitting model:

def get_shift_date(self):
            return self.sit_date.shift_date 

And this shift_date to be saved in the Sitting model. I define following save method:

 def save(self, *args, **kwargs):     

            self.sit_date = self.get_shift_date()
        super(Sitting, self).save(*args, **kwargs)

But this does not return shift dates as sitting dates. Could someone help me to fix this codes?

解决方案

You have contradictory if statements here. Your save method only calls get_shift_date if sit_date is blank; but inside that method, you only return something is sit_date is not blank. So sit_date can only ever be set to None.

这篇关于转换日期不作为日期更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆