Related
i made crawling and saving the info to django.db function with multiprocessing.
it works fine at first but gives error later.
when i launch stockPriceUpdate() on my desktop, it doesn't occur any errors. but when i launch on my laptop, it works fine at first but laters, it's speed slows down and gives error like below.
Stock is my django model
mycode
def stockPriceCrawling(stockTitle):
url = 'http://asp1.krx.co.kr/servlet/krx.asp.XMLSiseEng?code=' + str(stockTitle)
html = requests.get(url).content
soup = BeautifulSoup(html, 'html.parser')
stockinfo = soup.select('TBL_StockInfo')[0]
price = stockinfo['curjuka'].replace(',','')
highestPrice = stockinfo['highjuka'].replace(',','')
lowestPrice = stockinfo['lowjuka'].replace(',','')
tradeVolume = stockinfo['volume'].replace(',','')
tradeValue = stockinfo['money'].replace(',','')
print("info : ",' ', price," ",highestPrice," ",lowestPrice," ",tradeVolume," ",tradeValue)
db.connections.close_all()
stock= Stock.objects.get(code=stockTitle)
stock.price = price
stock.highestPrice = highestPrice
stock.lowestPrice = lowestPrice
stock.tradeVolume = tradeVolume
stock.tradeValue = tradeValue
stock.save()
def stockPriceUpdate(process=32):
# start = time.time()
stocks = Stock.objects.all()
stockTitles=[]
for stock in stocks:
stockTitles.append(str(stock.code))
pool = Pool(32)
r=pool.map(stockPriceCrawling, stockTitles)
pool.close()
pool.join()
error
Traceback (most recent call last):
File "/mnt/c/Users/DAUN/Desktop/swpp/swpp/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/mnt/c/Users/DAUN/Desktop/swpp/swpp/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 413, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: disk I/O error
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/multiprocessing/pool.py", line 121, in worker
result = (True, func(*args, **kwds))
File "/usr/local/lib/python3.7/multiprocessing/pool.py", line 44, in mapstar
return list(map(*args))
File "price_paser.py", line 84, in stockPriceCrawling
stock.save()
File "/mnt/c/Users/DAUN/Desktop/swpp/swpp/lib/python3.7/site-packages/django/db/models/base.py", line 754, in save
force_update=force_update, update_fields=update_fields)
File "/mnt/c/Users/DAUN/Desktop/swpp/swpp/lib/python3.7/site-packages/django/db/models/base.py", line 792, in save_base
force_update, using, update_fields,
File "/mnt/c/Users/DAUN/Desktop/swpp/swpp/lib/python3.7/site-packages/django/db/models/base.py", line 873, in _save_table
forced_update)
File "/mnt/c/Users/DAUN/Desktop/swpp/swpp/lib/python3.7/site-packages/django/db/models/base.py", line 926, in _do_update
return filtered._update(values) > 0
File "/mnt/c/Users/DAUN/Desktop/swpp/swpp/lib/python3.7/site-packages/django/db/models/query.py", line 803, in _update
return query.get_compiler(self.db).execute_sql(CURSOR)
File "/mnt/c/Users/DAUN/Desktop/swpp/swpp/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1522, in execute_sql
cursor = super().execute_sql(result_type)
File "/mnt/c/Users/DAUN/Desktop/swpp/swpp/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1156, in execute_sql
cursor.execute(sql, params)
File "/mnt/c/Users/DAUN/Desktop/swpp/swpp/lib/python3.7/site-packages/django/db/backends/utils.py", line 98, in execute
return super().execute(sql, params)
File "/mnt/c/Users/DAUN/Desktop/swpp/swpp/lib/python3.7/site-packages/django/db/backends/utils.py", line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/mnt/c/Users/DAUN/Desktop/swpp/swpp/lib/python3.7/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/mnt/c/Users/DAUN/Desktop/swpp/swpp/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/mnt/c/Users/DAUN/Desktop/swpp/swpp/lib/python3.7/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/mnt/c/Users/DAUN/Desktop/swpp/swpp/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
File "/mnt/c/Users/DAUN/Desktop/swpp/swpp/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 413, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: disk I/O error
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "price_paser.py", line 166, in <module>
stockPriceUpdate()
File "price_paser.py", line 97, in stockPriceUpdate
r=pool.map(stockPriceCrawling, stockTitles)
File "/usr/local/lib/python3.7/multiprocessing/pool.py", line 268, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
File "/usr/local/lib/python3.7/multiprocessing/pool.py", line 657, in get
raise self._value
django.db.utils.OperationalError: disk I/O error
The following code worked fine in Django 1.11, but in DJango 2 and up it fails:
'imageToLoad' : imageQ[0].imagePath
The error is AttributeError: 'ImageFieldFile' object has no attribute 'replace'
imagePath is an ImageField.
I was reading through the DJango 2 documentation but couldn't figure out what the issue is.
Here is the entire code block:
def getContextFromObject(request,imageQ):
if (imageQ != None):
imageJson = serializers.serialize("json", imageQ)
print("image id is " + str(imageQ[0].id))
request.session['image_id'] = imageQ[0].id
print("attempt to load: " + str(imageQ[0].imagePath))
# Below is the block that fails on DJango 2. Specifically, it is the first part, 'imageToLoad' : imageQ[0].imagePath
context = {'imageToLoad' : imageQ[0].imagePath,
'imageWidthPixels': imageQ[0].widthPixels,
'imageHeightPixels': imageQ[0].heightPixels,
'imageMeasuredCenter': (imageQ[0].swMeasuredCenter).quantize(Decimal('.01'), rounding=ROUND_DOWN),
'micronsPerPixel': imageQ[0].micronsPerPixel,
'imageChannelSelect': imageQ[0].channelSelect,
'imageId' : str(imageQ[0].id),
'images' : imageJson
}
else:
context = { }
return context;
Here is the full error traceback:
Traceback (most recent call last):
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\wsgiref\handlers.py", line 137, in run
self.result = application(self.environ, self.start_response)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\staticfiles\handlers.py", line 66, in __call__
return self.application(environ, start_response)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\wsgi.py", line 146, in __call__
response = self.get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py", line 81, in get_response
response = self._middleware_chain(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
response = response_for_exception(request, exc)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\deprecation.py", line 95, in __call__
response = self.get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
response = response_for_exception(request, exc)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\deprecation.py", line 95, in __call__
response = self.get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
response = response_for_exception(request, exc)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\deprecation.py", line 95, in __call__
response = self.get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
response = response_for_exception(request, exc)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\deprecation.py", line 95, in __call__
response = self.get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
response = response_for_exception(request, exc)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\deprecation.py", line 95, in __call__
response = self.get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
response = response_for_exception(request, exc)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\deprecation.py", line 95, in __call__
response = self.get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
response = response_for_exception(request, exc)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\deprecation.py", line 95, in __call__
response = self.get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 37, in inner
response = response_for_exception(request, exc)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 87, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\vistagalaxy\images\views.py", line 717, in currentViewing
return render(request, template, context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\shortcuts.py", line 36, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 175, in render
return self._render(context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 167, in _render
return self.nodelist.render(context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 943, in render
bit = node.render_annotated(context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 910, in render_annotated
return self.render(context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\loader_tags.py", line 155, in render
return compiled_parent._render(context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 167, in _render
return self.nodelist.render(context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 943, in render
bit = node.render_annotated(context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 910, in render_annotated
return self.render(context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\loader_tags.py", line 67, in render
result = block.nodelist.render(context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 943, in render
bit = node.render_annotated(context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\template\base.py", line 910, in render_annotated
return self.render(context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\templatetags\static.py", line 106, in render
url = self.url(context)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\templatetags\static.py", line 103, in url
return self.handle_simple(path)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\templatetags\static.py", line 118, in handle_simple
return staticfiles_storage.url(path)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\files\storage.py", line 329, in url
url = filepath_to_uri(name)
File "C:\Users\abharadwaj\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\utils\encoding.py", line 252, in filepath_to_uri
return quote(path.replace("\\", "/"), safe="/~!*()'")
AttributeError: 'ImageFieldFile' object has no attribute 'replace'
[14/Mar/2018 10:47:00] "GET /viewing HTTP/1.1" 500 59
EDIT: I've changed the title to better represent the solution I seeking.
I'm repeating a block of code and would like to a write a function to reduce clutter. I'm having trouble passing in the kwarg key hourly_id (last line of the original code).
Original Code
# Create Hourly data.
hourly_data = validated_data.pop('hourly')
hourly_points_data = hourly_data.pop('data')
hourly = Hourly.objects.create(**hourly_data)
for hourly_point_data in hourly_points_data:
hourly_point = HourlyPoint.objects.create(
hourly_id=hourly.pk, **hourly_point_data) <-- This
New Function
def create_data_block(self, data, block_str, DataBlock, DataPoint, id):
block_data = data.pop(block_str)
points_data = block_data.pop('data')
block = DataBlock.objects.create(**block_data)
for point_data in points_data:
point = DataPoint.objects.create(
id=block.pk, **point_data) <-- This
Function Call
self.create_data_block(validated_data, 'hourly', Hourly, HourlyPoint, 'hourly_id')
So you can see here I am trying to pass hourly_id as id using a string, but I get a database error saying that that hourly_id was missing so I'm clearly not passing it in correctly.
Traceback
Traceback (most recent call last):
File "/home/cudb/.virtualenvs/otto/lib/python3.5/site-packages/django/core/handlers/exception.py", line 42, in inner
response = get_response(request)
File "/home/cudb/.virtualenvs/otto/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/cudb/.virtualenvs/otto/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/lib/python3.5/contextlib.py", line 30, in inner
return func(*args, **kwds)
File "/home/cudb/.virtualenvs/otto/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/home/cudb/.virtualenvs/otto/lib/python3.5/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/cudb/.virtualenvs/otto/lib/python3.5/site-packages/rest_framework/views.py", line 489, in dispatch
response = self.handle_exception(exc)
File "/home/cudb/.virtualenvs/otto/lib/python3.5/site-packages/rest_framework/views.py", line 449, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/cudb/.virtualenvs/otto/lib/python3.5/site-packages/rest_framework/views.py", line 486, in dispatch
response = handler(request, *args, **kwargs)
File "/home/cudb/Projects/a/b/c/weather/views.py", line 36, in get
response = self.get_entry(latitude, longitude)
File "/home/cudb/Projects/a/b/c/weather/views.py", line 59, in get_entry
response = self.create_or_update_entry(latitude, longitude)
File "/home/cudb/Projects/a/b/c/weather/views.py", line 76, in create_or_update_entry
location_serializer.save()
File "/home/cudb/.virtualenvs/otto/lib/python3.5/site-packages/rest_framework/serializers.py", line 215, in save
self.instance = self.create(validated_data)
File "/home/cudb/Projects/a/b/c/weather/serializers.py", line 119, in create
validated_data, 'hourly', Hourly, HourlyPoint, 'hourly_id')
File "/home/cudb/Projects/a/b/c/weather/serializers.py", line 169, in create_data_block
id=block.pk, **point_data)
File "/home/cudb/.virtualenvs/otto/lib/python3.5/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/cudb/.virtualenvs/otto/lib/python3.5/site-packages/django/db/models/query.py", line 399, in create
obj.save(force_insert=True, using=self.db)
File "/home/cudb/.virtualenvs/otto/lib/python3.5/site-packages/django/db/models/base.py", line 796, in save
force_update=force_update, update_fields=update_fields)
File "/home/cudb/.virtualenvs/otto/lib/python3.5/site-packages/django/db/models/base.py", line 824, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/cudb/.virtualenvs/otto/lib/python3.5/site-packages/django/db/models/base.py", line 908, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/home/cudb/.virtualenvs/otto/lib/python3.5/site-packages/django/db/models/base.py", line 947, in _do_insert
using=using, raw=raw)
File "/home/cudb/.virtualenvs/otto/lib/python3.5/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/cudb/.virtualenvs/otto/lib/python3.5/site-packages/django/db/models/query.py", line 1045, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/cudb/.virtualenvs/otto/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1054, in execute_sql
cursor.execute(sql, params)
File "/home/cudb/.virtualenvs/otto/lib/python3.5/site-packages/debug_toolbar/panels/sql/tracking.py", line 164, in execute
return self._record(self.cursor.execute, sql, params)
File "/home/cudb/.virtualenvs/otto/lib/python3.5/site-packages/debug_toolbar/panels/sql/tracking.py", line 106, in _record
return method(sql, params)
File "/home/cudb/.virtualenvs/otto/lib/python3.5/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/cudb/.virtualenvs/otto/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/cudb/.virtualenvs/otto/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/cudb/.virtualenvs/otto/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/cudb/.virtualenvs/otto/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: null value in column "hourly_id" violates not-null constraint
DETAIL: Failing row contains (6, 76.22, 77.31, 0.80, 0.00, clear-night, Clear, null).
[04/Aug/2017 12:05:30] "GET /weather/0,4 HTTP/1.1" 500 288106
This where I see a problem:
hourly_point = HourlyPoint.objects.create(hourly_id=hourly.pk, **hourly_point_data)
point = DataPoint.objects.create(id=block.pk, **point_data)
You can rename the variables to be more generic as you see fit, but hourly_id is not a variable, it's a named parameter. By using id instead, you're not passing in the expected hourly_id argument but passing in a potentially unwanted, or incorrect, id argument.
Unless you change the definition of create() itself, the argument name remains hourly_id. One way around this is to use the argument positionally, if possible, instead of by name. (I.e. is it a keyword argument only or a positional argument being accessed by keyword?)
Alternatively, if you pass in 'hourly_id' as id then augment point_data with this key and value:
def create_data_block(self, data, block_str, DataBlock, DataPoint, id):
block_data = data.pop(block_str)
points_data = block_data.pop('data')
block = DataBlock.objects.create(**block_data)
for point_data in points_data:
point = DataPoint.objects.create(**{id: block.pk, **point_data})
I am trying to update a self datetime field of a model using F() object in django.
HeatWatchList.objects.filter(
user=request.user,
next_date_to__lt=datetime.combine(datetime.now().date(), time.min)
).update(
next_date_from = F('next_date_from') + relativedelta(months=1),
next_date_to = F('next_date_to') + relativedelta(months=1)
)
But when doing this i got AttributeError: relativedelta object has no attribute translate.
Here is the traceback i got when executing the code maybe it has an issue using the F() object when adding value with a datetime type relativedelta.
Traceback (most recent call last):
File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\django\core\handlers\exception.py", line 39, in inner
response = get_response(request)
File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\django\views\decorators\csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\rest_framework\viewsets.py", line 83, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\rest_framework\views.py", line 477, in dispatch
response = self.handle_exception(exc)
File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\rest_framework\views.py", line 437, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\rest_framework\views.py", line 474, in dispatch
response = handler(request, *args, **kwargs)
File "C:\Users\Web\Desktop\PyDev\Envs\djangular\farm_management\heat\views.py", line 192, in update_watchlist
next_date_to = F('next_date_to') + relativedelta(months=1)
File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\django\db\models\query.py", line 637, in update
rows = query.get_compiler(self.db).execute_sql(CURSOR)
File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\django\db\models\sql\compiler.py", line 1148, in execute_sql
cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\django\db\models\sql\compiler.py", line 835, in execute_sql
cursor.execute(sql, params)
File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\django\db\backends\utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\django\db\backends\mysql\base.py", line 110, in execute
return self.cursor.execute(query, args)
File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\pymysql\cursors.py", line 164, in execute
query = self.mogrify(query, args)
File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\pymysql\cursors.py", line 143, in mogrify
query = query % self._escape_args(args, conn)
File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\pymysql\cursors.py", line 118, in _escape_args
return tuple(conn.literal(arg) for arg in args)
File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\pymysql\cursors.py", line 118, in <genexpr>
return tuple(conn.literal(arg) for arg in args)
File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\pymysql\connections.py", line 800, in literal
return self.escape(obj, self.encoders)
File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\pymysql\connections.py", line 793, in escape
return escape_item(obj, self.charset, mapping=mapping)
File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\pymysql\converters.py", line 27, in escape_item
val = encoder(val, mapping)
File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\pymysql\converters.py", line 110, in escape_unicode
return u"'%s'" % _escape_unicode(value)
File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\pymysql\converters.py", line 73, in _escape_unicode
return value.translate(_escape_table)
AttributeError: 'relativedelta' object has no attribute 'translate'
Seems like pymysql's converters does not support relativedelta objects.
One possible solution is to iterate over objects and update it (inefficient):
objects_list = HeatWatchList.objects.filter(
user=request.user,
next_date_to__lt=datetime.combine(datetime.now().date(), time.min)
)
for obj in object_list:
obj.next_date_from = onj.next_date_from + relativedelta(months=1)
obj.next_date_to = onj.next_date_to + relativedelta(months=1)
objsave()
Or better use datetime.timedelta instead of relativedelta if it possible:
HeatWatchList.objects.filter(
user=request.user,
next_date_to__lt=datetime.combine(datetime.now().date(), time.min)
).update(
next_date_from = F('next_date_from') + timedelta(days=31),
next_date_to = F('next_date_to') + timedelta(days=31)
)
I am trying to run a very simple test using django:
class PaymentTests(TestCase):
def setUp(self):
self.c = Client()
def test_can_see_chat_for_an_anynomous_user(self):
response = self.c.get('/')
self.assertEqual(200, response.status_code)
Unfortunately each time I try to run it I get an error:
(project)linoor#linoor-virtual-machine /mnt/hgfs/Dev/LegalStart/project $ python manage.py test --rednose useracts.tests:PaymentTests
nosetests useracts.tests:PaymentTests --rednose --verbosity=1
Creating test database for alias 'default'...
ERROR 2015-02-11 16:37:00,082 Internal Server Error: /
Traceback (most recent call last):
File "/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 105, in get_response
response = middleware_method(request, callback, callback_args, callback_kwargs)
File "/mnt/hgfs/Dev/LegalStart/project/staticpages/middleware.py", line 56, in process_view
r = Redirect.objects.get(old_path='/' + slug)
File "/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/models/manager.py", line 151, in get
return self.get_queryset().get(*args, **kwargs)
File "/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/models/query.py", line 304, in get
num = len(clone)
File "/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/models/query.py", line 77, in __len__
self._fetch_all()
File "/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/models/query.py", line 857, in _fetch_all
self._result_cache = list(self.iterator())
File "/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/models/query.py", line 220, in iterator
for row in compiler.results_iter():
File "/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 713, in results_iter
for rows in self.execute_sql(MULTI):
File "/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
cursor.execute(sql, params)
File "/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
File "/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/utils.py", line 99, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
ProgrammingError: relation "django_redirect" does not exist
LINE 1: ...ct"."old_path", "django_redirect"."new_path" FROM "django_re...
^
X
-----------------------------------------------------------------------------
1) ERROR: test_can_see_chat_for_an_anynomous_user (useracts.tests.PaymentTests)
Traceback (most recent call last):
useracts/tests.py line 48 in test_can_see_chat_for_an_anynomous_user
response = self.c.post('/')
/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/test/client.py line 483 in post
response = super(Client, self).post(path, data=data, content_type=content_type, **extra)
/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/test/client.py line 302 in post
return self.request(**r)
/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/test/client.py line 444 in request
six.reraise(*exc_info)
/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/core/handlers/base.py line 105 in get_response
response = middleware_method(request, callback, callback_args, callback_kwargs)
staticpages/middleware.py line 56 in process_view
r = Redirect.objects.get(old_path='/' + slug)
/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/models/manager.py line 151 in get
return self.get_queryset().get(*args, **kwargs)
/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/models/query.py line 304 in get
num = len(clone)
/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/models/query.py line 77 in __len__
self._fetch_all()
/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/models/query.py line 857 in _fetch_all
self._result_cache = list(self.iterator())
/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/models/query.py line 220 in iterator
for row in compiler.results_iter():
/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py line 713 in results_iter
for rows in self.execute_sql(MULTI):
/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py line 786 in execute_sql
cursor.execute(sql, params)
/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/backends/util.py line 53 in execute
return self.cursor.execute(sql, params)
/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/utils.py line 99 in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/backends/util.py line 53 in execute
return self.cursor.execute(sql, params)
ProgrammingError: relation "django_redirect" does not exist
LINE 1: ...ct"."old_path", "django_redirect"."new_path" FROM "django_re...
^
-------------------- >> begin captured logging << --------------------
django.request: ERROR: Internal Server Error: /
Traceback (most recent call last):
File "/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 105, in get_response
response = middleware_method(request, callback, callback_args, callback_kwargs)
File "/mnt/hgfs/Dev/LegalStart/project/staticpages/middleware.py", line 56, in process_view
r = Redirect.objects.get(old_path='/' + slug)
File "/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/models/manager.py", line 151, in get
return self.get_queryset().get(*args, **kwargs)
File "/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/models/query.py", line 304, in get
num = len(clone)
File "/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/models/query.py", line 77, in __len__
self._fetch_all()
File "/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/models/query.py", line 857, in _fetch_all
self._result_cache = list(self.iterator())
File "/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/models/query.py", line 220, in iterator
for row in compiler.results_iter():
File "/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 713, in results_iter
for rows in self.execute_sql(MULTI):
File "/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
cursor.execute(sql, params)
File "/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
File "/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/utils.py", line 99, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/linoor/virtualenvs/LegalStart/project/local/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
ProgrammingError: relation "django_redirect" does not exist
LINE 1: ...ct"."old_path", "django_redirect"."new_path" FROM "django_re...
^
--------------------- >> end captured logging << ---------------------
I've tried doing the same thing in the django shell and it works fine.
The project uses South and I found somewhere that I should add these lines to the project.py file:
SOUTH_TESTS_MIGRATE = False
SKIP_SOUTH_TESTS = True
But it still doesn't work.
Do you guys have any idea what might be wrong? I can provide additional info if needed.
Thanks!
Ok, so the problem was Django Mezzanine addon which was dynamically removing the "django.contrib.redirects" from the INSTALLED_APPS.