pythondjangodjango-modelsmysql-pythondjango-1.6

Can't pass null fields into django floatfield?


(approach inspired by this thread)

I have some json data that I want to import into django models using the example data:

[{"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}

This is my model for Member thus far:

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

And this is the script that loads the member-related fields (from the json file) into my Member model:

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()

However, I get this error:

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

I've already specified null=True and blank=True in the member_loc_lat and member_loc_long variables in my model but I'm not quite sure what to do. Should I specify that these fields should accept null in a MySQL script?

BTW, I'm using Django 1.6.11. Thanks!


Solution

  • You may need to run a migrate with python manage.py migrate.
    Any sort of schema related changes like changing fields, tables, etc. needs a migrate.