Django - 'NoneType'对象在迁移后只对某些用户没有属性'replace'吗?

时间:2016-02-20 22:08:22

标签: django migration django-south

最近,我一直致力于一个系统,我们将应用程序从较旧的Django版本(使用South进行迁移)更新为最新版本(使用集成迁移)。

今天早上,在服务器上工作的另一个人说迁移存在问题,因为南方迁移中的某些内容未被正确考虑,因此他不得不做一些额外的步骤来迁移数据库正确。

所以我今天早上检查了申请,事情对我来说很好。但网站所有者从她的用户帐户报告了问题。她尝试登录她拥有的另一个用户帐户,并且工作正常。但她的主要帐户收到以下错误:

'NoneType' object has no attribute 'replace'

在行......

<label>Age: <span class="uneditable-input input-mini form-control"> {{ visit.patient_age_at_time_of_visit }}

以下是与之相关的模型部分:

@property
def patient_age_at_time_of_visit(self):
    today = self.visit_date
    born = self.patient.dob
    try: 
        birthday = born.replace(year=today.year)
    except ValueError: # raised when birth date is February 29 and the current year is not a leap year
        birthday = born.replace(year=today.year, day=born.day-1)
    return today.year - born.year - (birthday > today)

知道如何修复此问题,和/或为什么它只会影响certian用户帐户?后一个问题让我更担心......

2 个答案:

答案 0 :(得分:0)

某些患者帐户显然有dob的空字段。

答案 1 :(得分:0)

正如丹尼尔所说,你显然有空的dob字段,但是如果你想“修复”这个确切的问题,你需要检查None并做一些事情:

@property
def patient_age_at_time_of_visit(self):
    today = self.visit_date
    born = self.patient.dob

    if born is None:
        return None

    try: 
        birthday = born.replace(year=today.year)
    except ValueError: # raised when birth date is February 29 and the current year is not a leap year
        birthday = born.replace(year=today.year, day=born.day-1)
    return today.year - born.year - (birthday > today)

更新

我猜你正在做以下事情:

您展示的属性是某些模型的一部分,可能称为“访问”。 病人也是模特。 dob是Patient模型的一个字段。

你最有可能像这样分配dob:

visit = Visit.objects.get(id=some_id)
visit.patient.dob = some_dob
visit.save()

这实际上不起作用。你必须拯救病人,而不是访问。所以它应该是:

visit = Visit.objects.get(id=some_id)
visit.patient.dob = some_dob
visit.patient.save()

你甚至不需要保存访问,因为技术上没有任何改变。

更新

在注意到它必须被调用之后,编辑上面的更新从“A”到访问。