Using Django create() on a time.field

23 views
Skip to first unread message

Malik Rumi

unread,
Jan 7, 2018, 4:03:05 PM1/7/18
to Django users

I am trying to process some text files and feed the result directly into a django model using create(). As so often seems to happen with me, this turns out to not be as simple as I thought it would be. Specifically, I can’t seem to get any data into ‘clock’, the time field in my model. When I tried making the input data a string, it rejected it. When I tried making it a function, it told me it was expecting a string, and no version of int, float, or decimal got past Pylint. What is the right way to get this done, or do I need to do direct db inserts instead? Here is my code. Thanks.


As a string ***

from io import TextIOBase

from os import environ

import textract

import django

environ['DJANGO_SETTINGS_MODULE'] = 'chronicle.settings'

django.setup()

from ktab.models import Entry



text = textract.process('/home/malikarumi/010417_odt_tests/2018-01-01 psycopg2-error-at-or-near.odt', encoding='utf_8')

b = TextIOBase(text)

b.write(Entry.objects.create(

   title='2018-01-01 psycopg2-error-at-or-near', content=b, chron_date='2018-01-05',

   clock='23:59:59'))

b.save()


(lifeandtimes) malikarumi@Tetuoan2:~/Projects/lifeandtimes/chronicle$ python django_textract_7.py

Traceback (most recent call last):

 File "django_textract_7.py", line 17, in <module>

   clock='23:59:59'))

io.UnsupportedOperation: write


--

As a function ***

from time import time, localtime

from io import TextIOBase

from os import environ

import textract

import django

environ['DJANGO_SETTINGS_MODULE'] = 'chronicle.settings'

django.setup()

from ktab.models import Entry



text = textract.process('/home/malikarumi/010417_odt_tests/2018-01-01 psycopg2-error-at-or-near.odt', encoding='utf_8')

b = TextIOBase(text)

b.write(Entry.objects.create(

   title='2018-01-01 psycopg2-error-at-or-near', content=str(b), chron_date='2018-01-05',

   clock=localtime(time())))

b.save()


(lifeandtimes) malikarumi@Tetuoan2:~/Projects/lifeandtimes/chronicle$ python django_textract_10.py

Traceback (most recent call last):

 File "django_textract_10.py", line 18, in <module>

   clock=localtime(time())))

<...snip…>

File "/home/malikarumi/Projects/lifeandtimes/lib/python3.6/site-packages/django/utils/dateparse.py", line 76, in parse_time

   match = time_re.match(value)

TypeError: expected string or bytes-like object

(lifeandtimes) malikarumi@Tetuoan2:~/Projects/lifeandtimes/chronicle$


--

As a string that evaluates to the past ***

(lifeandtimes) malikarumi@Tetuoan2:~/Projects/lifeandtimes/chronicle$ python django_textract_9.py

Traceback (most recent call last):

 File "django_textract_9.py", line 17, in <module>

   clock='04:04:04'))

io.UnsupportedOperation: write


***Back to the same old error. How can it not support writing to a single field when it writes to all the others? ***

Django 1.11.5 Postgres 9.4, Psycopg2, Python 3.6.3

Daniel Roseman

unread,
Jan 7, 2018, 4:09:04 PM1/7/18
to Django users
On Sunday, 7 January 2018 21:03:05 UTC, Malik Rumi wrote:

--

As a string that evaluates to the past ***

(lifeandtimes) malikarumi@Tetuoan2:~/Projects/lifeandtimes/chronicle$ python django_textract_9.py

Traceback (most recent call last):

 File "django_textract_9.py", line 17, in <module>

   clock='04:04:04'))

io.UnsupportedOperation: write


***Back to the same old error. How can it not support writing to a single field when it writes to all the others? ***

Django 1.11.5 Postgres 9.4, Psycopg2, Python 3.6.3


The error is nothing to do with the time field. The model create command is *succeeding*, as you could tell by looking at the database.

However you have wrapped this creation in a TextIOBase.write function, which is failing. This is because a TextIOBase is an abstract class which does not actually have any concept of anything to write to. What, exactly, are you trying to do here?
-- 
DR.
Reply all
Reply to author
Forward
0 new messages