I'm using get_or_create as I iterate through a CSV file to read them into the database. With logging enabled I use the object, created tuple to note how it's getting along in the logfile - that all works most of the time, except occasionally it throws a 'tuple object not callable' on the 'created' variable returned by get_or_create. It doesn't seem to be consistent in where it throws it - if I run the same code on the same file, sometimes it goes past it and works fine, other times it sticks on (different) earlier lines of the CSV.
edit: The error falls when I call 'created' - so on the logger.debug(office.name
section, for example.
As a workaround, I have a catchall exception that, as part of the 'except' function, retries the same get_or_create. This fallback is working, and hasn't excepted yet (I'm up to 67k records, and it's thrown a different error), but it's a hatchet job, and very unpythonic. I'd much rather fix the fundamental problem - it feels like get_or_create is silently failing, which means that the tuple is broken, but I can't find any evidence of that; surely if that's the case, an exceptable (sp?) error would be thrown. (There are no duplicates that would result in MultipleObjectsReturned, and I'm not getting an exception anyway).
Tl;dr - get_or_create works 99% of the time, and all the time if I put a fallback in, but doesn't fail in a helpful way.
Suggestions warmly welcomed.
Code is as below:
try:
office, created = Office.objects.get_or_create(name=office_name)
logger.debug(office)
logger.debug(created)
if created:
logger.info("Office added to database:")
logger.info(office.name)
else:
logger.info("Office already exists:")
logger.info(office.name)
except: #catchall exception for now
logger.error("Could not create: %s", office_name)
office, created = Office.objects.get_or_create(name=office_name)
if created:
logger.info("Office added to database:")
logger.info(str(office.name))
else:
logger.info("Office already exists:")
logger.info(str(office.name))
return office
return office
Traceback (from logs only, apologies):
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py", line 112, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/~/Django/OMS/core/views.py", line 28, in loadinitialstats
load_stats_from_csv(location)
File "/Users/~/Django/OMS/core/filehandling.py", line 139, in load_stats_from_csv
home_office = fetch_or_create_office(row[2])
File "/Users/~/Django/OMS/core/filehandling.py", line 13, in fetch_or_create_office
logger.info(created)
TypeError: 'tuple' object is not callable
logger.info(created)
anywhere.