无法将 null 字段传递给 django floatfield?

Can't pass null fields into django floatfield?

(受此 thread 启发的方法)

我有一些 json 数据要使用示例数据导入到 Django 模型中:

[{"orderID": "2b062ac4542172b12f47e2b26d1629ae", "status": "No Car", "specialRequest": "", "endedTime": "2018-12-01T00:02:34HKT", "tunnel": "", "pickedTime": null, "toLocation": {"lat": "22.307488971554207", "lng": "114.2600533354737", "displayName": "\u5c07\u8ecd\u6fb3", "locality": "\u5c07\u8ecd\u6fb3"}, "memberID": "c8dfb02f6d81394582f3ceac57749616", "fromLocation": {"lat": "22.3172022", "lng": "114.16995739999993", "displayName": "\u5f4c\u6566\u9053\u502b\u6566\u5927\u9152\u6a13", "locality": "\u65fa\u89d2"}, "specialRequestText": "85\u6298", "cancelReason": null, "createdTime": "2018-12-01T00:00:00HKT", "paymentMethod": "Cash", "extraTips": 0}

到目前为止,这是我的会员模型:

class Member(models.Model):
    member_id = models.CharField(max_length=32, primary_key=True)
    member_loc_lat = models.FloatField(default= None, null=True, blank=True) 
    member_loc_long = models.FloatField(default= None, null=True, blank=True) # blank to be able to pass in null values, null to tell the DB to accept null values
    member_locality = models.TextField() 
    member_display_name = models.TextField()
    member_created_time = models.DateTimeField(auto_now=False, auto_now_add=False)

    def __str__(self):
        return self.member_id

这是将会员相关字段(从 json 文件)加载到我的会员模型中的脚本:

import json
import argparse
import django
from django.conf import settings
import sys
import os
import pymysql
pymysql.install_as_MySQLdb()

from mysite.models import Member, Driver, Order


def main():
    parser = argparse.ArgumentParser()

    parser.add_argument("-i", "--input", type=str, required=True, help="path to original data file (.data)")
    args = parser.parse_args()
    
    data_members = {} # type list
    
    with open(args.input) as f:
        data_members = json.load(f)
    
    for member in range(len(data_members)):
        #print(data_members[0]['memberID'])
        if 'memberID' in data_members[member]:
            Member.objects.get_or_create(
               member_id = data_members[member]['memberID'],
            )
        elif 'createdTime' in data_members[member]:
            Member.objects.get_or_create(
                 member_created_time = data_members[member]['createdTime'],
             )
        elif 'displayName' in data_members[member]: 
            Member.objects.get_or_create(
                member_display_name = data_members[member]['displayName'],
            )

        elif 'toLocation' in data_members[member]:
            Member.objects.get_or_create(
                member_loc_lat = data_members[member]['toLocation']['lat'],
                member_loc_long = data_members[member]['toLocation']['lng'],
            )
        elif 'locality' in data_members[member]:  
            Member.objects.get_or_create(
                member_locality = data_members[member]['locality'],
           )

if __name__ == "__main__":
    main()

但是,我得到这个错误:

django.db.utils.IntegrityError: (1048, u"Column 'member_loc_lat' cannot be null")

我已经在模型的 member_loc_latmember_loc_long 变量中指定了 null=Trueblank=True,但我不太确定该怎么做。我应该在 MySQL 脚本中指定这些字段应该接受 null 吗?

顺便说一句,我使用的是 Django 1.6.11。谢谢!

您可能需要 运行 迁移 python manage.py migrate
任何类型的架构相关更改(如更改字段、表等)都需要迁移。