Django / xhtml2pdf - object has no attribute 'encode' - python

I'm having a hard time producing pdf's in Django. As per my previous question I'm running into the same error.
When I run the following code I get 'list' object has no attribute 'encode', a pdf is saved in the media folder but it is a plain text file.
I think the object that is being referred to is the sections queryset.
#login_required
def generate_pdf(request, slug):
# Prepare context
document = get_object_or_404(Document, slug=slug)
sections = \
get_list_or_404(Section.objects.filter
(associated_document__startswith=document.slug))
data = {'document': document, 'sections': sections}
# Render html content through html template with context
template = get_template('lld/lld_pdf.html')
html = template.render(Context(data))
file = open('/home/project/media/test.pdf', "w+b")
print type(document)
print type(sections)
print type(data)
print type(template)
print type(html)
print type(file)
pisaStatus = pisa.CreatePDF(html.encode('utf-8'), dest=file)
# Return PDF document through a Django HTTP response
file.seek(0)
pdf = file.read()
file.close() # Don't forget to close the file handle
return HttpResponse(pdf, mimetype='application/pdf')
cli output:
<class 'lld.models.Document'>
<type 'list'>
<type 'dict'>
<class 'django.template.backends.django.Template'>
<class 'django.utils.safestring.SafeText'>
<type 'file'>
and the error:
Environment:
Request Method: GET
Request URL: http://localhost:8001/lld/tesco-greenfield-datacenter-deployment/pdf/
Django Version: 1.8.2
Python Version: 2.7.6
Installed Applications:
('django_admin_bootstrapped',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'lld',
'registration')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/decorators.py" in _wrapped_view
22. return view_func(request, *args, **kwargs)
File "/home/vagrant/shared/RepeatableDesign/lld/views.py" in generate_pdf
162. pisaStatus = pisa.CreatePDF(html.encode('utf-8'), dest=file)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/document.py" in pisaDocument
89. encoding, context=context, xml_output=xml_output)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/document.py" in pisaStory
57. pisaParser(src, context, default_css, xhtml, encoding, xml_output)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/parser.py" in pisaParser
685. context.parseCSS()
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/context.py" in parseCSS
498. self.css = self.cssParser.parse(self.cssText)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/w3c/cssParser.py" in parse
434. src, stylesheet = self._parseStylesheet(src)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/w3c/cssParser.py" in _parseStylesheet
522. src, stylesheetImports = self._parseAtImports(src)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/w3c/cssParser.py" in _parseAtImports
606. stylesheet = self.cssBuilder.atImport(import_, mediums, self)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/w3c/css.py" in atImport
874. return cssParser.parseExternal(import_)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/context.py" in parseExternal
380. result = self.parse(cssFile.getData())
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/w3c/cssParser.py" in parse
434. src, stylesheet = self._parseStylesheet(src)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/w3c/cssParser.py" in _parseStylesheet
533. src, atResults = self._parseAtKeyword(src)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/w3c/cssParser.py" in _parseAtKeyword
655. src, result = self._parseAtFontFace(src)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/w3c/cssParser.py" in _parseAtFontFace
785. result = [self.cssBuilder.atFontFace(properties)]
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/context.py" in atFontFace
173. src = self.c.getFile(data["src"], relative=self.c.cssParser.rootPath)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/context.py" in getFile
823. return getFile(name, relative or self.pathDirectory)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/util.py" in getFile
635. file = pisaFileObject(*a, **kw)
File "/usr/local/lib/python2.7/dist-packages/xhtml2pdf/util.py" in __init__
516. uri = uri.encode('utf-8')
Exception Type: AttributeError at /lld/tesco-greenfield-datacenter-deployment/pdf/
Exception Value: 'list' object has no attribute 'encode'

I can't even believe I've spent a few days crying and tearing my hair out because I was using a Google font. Yep, once I removed <link href="http://fonts.googleapis.com/css?family=Lato:400,700" rel="stylesheet" type="text/css"> from my template, the error vamoosed!

Related

TypeError: Object of type 'TopicSerializer' is not JSON serializable Django

Hi All I faced this issue while getting API response. I am using django==2.1.7. Below I am adding my code snippet
models.py
The model contains topic names(en,bn), taglist(string), and Bucket_id or topic_id
class Topics(models.Model):
id = models.AutoField(primary_key=True)
name_en = models.CharField(max_length=70, blank=False, default='')
name_bn = models.CharField(max_length=70, blank=False, default='')
tags = models.TextField(blank=False, default='')
bucket_id = models.IntegerField()
def set_tags(self, x):
self.tags = json.dumps(x)
def get_tags(self):
return json.loads(self.tags)
class Meta:
managed = True
db_table = 'follow_topics'
serialize.py
TopicSeializer used for JSON serialize the model
from rest_framework import serializers
from .models import Topics
class TopicSerializer(serializers.ModelSerializer):
class Meta:
model = Topics
fields = ['name_en','name_bn','tags', 'bucket_id']
views.py
get_topic function gives me the list of the topic from the model
#api_view(('GET',))
def get_topics(requests):
topic_id = requests.GET.get('topic_id', None)
post_data = Topics.objects.filter(bucket_id=topic_id)
serialize_data = TopicSerializer(post_data, many=True)
return Response({"status": "success", "data": serialize_data})
I got serializable error. This error looks frustrating for me. Please help me share some useful resource or way that I can fix this error
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/feed/v1/get_topics?topic_id=2
Django Version: 2.1.7
Python Version: 3.6.8
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'debug_toolbar',
'rest_framework',
'feedv1']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.BrokenLinkEmailsMiddleware',
'django.middleware.common.CommonMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware']
Traceback:
File "/Users/luo/tutorial-env/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/Users/luo/tutorial-env/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
156. response = self.process_exception_by_middleware(e, request)
File "/Users/luo/tutorial-env/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
154. response = response.render()
File "/Users/luo/tutorial-env/lib/python3.6/site-packages/django/template/response.py" in render
106. self.content = self.rendered_content
File "/Users/luo/tutorial-env/lib/python3.6/site-packages/rest_framework/response.py" in rendered_content
72. ret = renderer.render(self.data, accepted_media_type, context)
File "/Users/luo/tutorial-env/lib/python3.6/site-packages/rest_framework/renderers.py" in render
733. context = self.get_context(data, accepted_media_type, renderer_context)
File "/Users/luo/tutorial-env/lib/python3.6/site-packages/rest_framework/renderers.py" in get_context
688. 'content': self.get_content(renderer, data, accepted_media_type, renderer_context),
File "/Users/luo/tutorial-env/lib/python3.6/site-packages/rest_framework/renderers.py" in get_content
424. content = renderer.render(data, accepted_media_type, renderer_context)
File "/Users/luo/tutorial-env/lib/python3.6/site-packages/rest_framework/renderers.py" in render
107. allow_nan=not self.strict, separators=separators
File "/Users/luo/tutorial-env/lib/python3.6/site-packages/rest_framework/utils/json.py" in dumps
28. return json.dumps(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py" in dumps
238. **kw).encode(obj)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py" in encode
201. chunks = list(chunks)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py" in _iterencode
430. yield from _iterencode_dict(o, _current_indent_level)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py" in _iterencode_dict
404. yield from chunks
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py" in _iterencode
437. o = _default(o)
File "/Users/luo/tutorial-env/lib/python3.6/site-packages/rest_framework/utils/encoders.py" in default
68. return super(JSONEncoder, self).default(obj)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py" in default
180. o.__class__.__name__)
Exception Type: TypeError at /feed/v1/get_topics
Exception Value: Object of type 'TopicSerializer' is not JSON serializable
You need to access the .data property of the serializer
return Response({"status": "success", "data": serialize_data.data})

django template errno 13

I have a problem with django template. I made new template file in pycharm and in code i think all is good. But i have errno 13 permission denied. Other templates are working fine.
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/Doggo/2/results/
Django Version: 1.11
Python Version: 3.6.1
Installed Applications:
['Doggo.apps.DoggoConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Python36-32\lib\site-packages\django\core\handlers\exception.py" in inner
41. response = get_response(request)
File "C:\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
217. response = self.process_exception_by_middleware(e, request)
File "C:\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
215. response = response.render()
File "C:\Python36-32\lib\site-packages\django\template\response.py" in render
107. self.content = self.rendered_content
File "C:\Python36-32\lib\site-packages\django\template\response.py" in rendered_content
82. template = self.resolve_template(self.template_name)
File "C:\Python36-32\lib\site-packages\django\template\response.py" in resolve_template
64. return select_template(template, using=self.using)
File "C:\Python36-32\lib\site-packages\django\template\loader.py" in select_template
48. return engine.get_template(template_name)
File "C:\Python36-32\lib\site-packages\django\template\backends\django.py" in get_template
39. return Template(self.engine.get_template(template_name), self)
File "C:\Python36-32\lib\site-packages\django\template\engine.py" in get_template
162. template, origin = self.find_template(template_name)
File "C:\Python36-32\lib\site-packages\django\template\engine.py" in find_template
136. name, template_dirs=dirs, skip=skip,
File "C:\Python36-32\lib\site-packages\django\template\loaders\base.py" in get_template
38. contents = self.get_contents(origin)
File "C:\Python36-32\lib\site-packages\django\template\loaders\filesystem.py" in get_contents
28. with io.open(origin.name, encoding=self.engine.file_charset) as fp:
Exception Type: PermissionError at /Doggo/2/results/
Exception Value: [Errno 13] Permission denied: 'C:\\Users\\noob1_000\\PycharmProjects\\eWybory\\Doggo\\templates'
i think i have permission to this folder because other templates are working.
I'm working on windows 10
I would recommend to go through this-
Also , the view that you uses the template are also in the same directory as other views since you said that other views are able to access their templates ?
Let me know , if this solves your problem
Maybe you are just forgetting to indicate the template path (template / app/index.html) in the views.py.

django: Unexpected data type exception trying to use django-excel

So I am trying to use django-excel: https://github.com/pyexcel/django-excel
and just simply export a certain model into xls. I set up the settings according to the documentation, but I got this error when running:
Exception at /calbase/export/sheet
Unexpected data type <class 'django.db.models.fields.files.FieldFile'>
Here is my views:
def export_data(request, atype):
if atype == "sheet":
return excel.make_response_from_a_table(
Equipment, 'xls', file_name="sheet")
elif atype == "book":
return excel.make_response_from_tables(
[Equipment, Calibration], 'xls', file_name="book")
elif atype == "custom":
equipment = Equipment.objects.get(slug='ide')
query_sets = Choice.objects.filter(equipment=equipment)
column_names = ['choice_text', 'id', 'votes']
return excel.make_response_from_query_sets(
query_sets,
column_names,
'xls',
file_name="custom"
)
else:
return HttpResponseBadRequest(
"Bad request. please put one of these " +
"in your url suffix: sheet, book or custom")
and when I tried to go
http://127.0.0.1:8000/calbase/export/sheet
it gives me the exception above.
By the way is there any good example using django-excel? I can't seem to run the one provided
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/calbase/export/sheet
Django Version: 1.10
Python Version: 3.5.2
Installed Applications:
['calbase.apps.CalbaseConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'haystack',
'whoosh',
'carton']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\exception.py" in inner
39. response = get_response(request)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\hansong.li\Documents\GitHub\equipCal\calbase\views.py" in export_data
97. Equipment, 'xls', file_name="sheet")
File "C:\Users\hansong.li\Documents\GitHub\equipCal\django_excel\__init__.py" in make_response_from_a_table
147. file_name=file_name, **keywords)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyexcel_webio\__init__.py" in make_response
244. file_stream = pyexcel_instance.save_to_memory(file_type, None, **keywords)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyexcel\sheets\sheet.py" in save_to_memory
114. self.save_to(out_source)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyexcel\sheets\sheet.py" in save_to
79. source.write_data(self)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyexcel\sources\file_source_output.py" in write_data
86. sheet, **self.keywords)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyexcel\sources\renderer\_excel.py" in render_sheet_to_stream
35. **keywords)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyexcel_io\io.py" in save_data
70. **keywords)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyexcel_io\io.py" in store_data
91. writer.write(data)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyexcel_io\book.py" in write
177. sheet_writer.write_array(incoming_dict[sheet_name])
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyexcel_io\sheet.py" in write_array
82. self.write_row(row)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pyexcel_xls\xls.py" in write_row
230. self.native_sheet.write(self.current_row, i, value)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\xlwt\Worksheet.py" in write
1033. self.row(r).write(c, label, style)
File "C:\Users\hansong.li\AppData\Local\Programs\Python\Python35-32\lib\site-packages\xlwt\Row.py" in write
263. raise Exception("Unexpected data type %r" % type(label))
Exception Type: Exception at /calbase/export/sheet
Exception Value: Unexpected data type <class 'django.db.models.fields.files.FieldFile'>

Attribute error queryset.filter() django

So I got a method in views.py that checks if a certain product is saved in the session variables. If this is the case then it will look up the ID of the product in mongodb in order to filter a queryset on a few attributes of this product. I have the following code:
def moederbordenComp(request,objectlijst):
objectlijst_filtered = objectlijst
if "processorenid" in request.session:
processor= Processoren.objects.get(id=request.session["processorenid"])
objectlijst_filtered = objectlijst.filter(Socket__icontains=processor.Socket)
if "behuizingenid" in request.session:
behuizing = Behuizingen.objects.get(id=request.session["behuizingenid"])
objectlijst_filtered = objectlijst.filter(Form_Factor__icontains=behuizing.Form_Factor)
if "geheugenid" in request.session:
geheugen = Geheugen.objects.get(id=request.session["geheugenid"])
objectlijst_filtered = objectlijst.filter(Geheugentype__icontains=geheugen.Geheugentype)
objectlijst_filtered = objectlijst.filter(Geheugentype__icontains=geheugen.Aantal)
if "hardeid" in request.session:
harde = Harde.objects.get(id=request.session["hardeid"])
objectlijst_filtered = objectlijst.filter(Hardeschijf_bus__icontains=harde.Hardeschijf_bus)
return objectlijst_filtered
So basically. If processorenid exists then filter the queryset called "objectlist" so that only the motherboards remain that contain the processor.Socket.
Now for the real issue:
whenever I remove the objectlijst_filtered and just return the objectlijst (so unfiltered, without doing anything) everything works fine. However if I try to return and then use objectlijst_filtered or even print objectlijst_filtered it raises the following error:'list' object has no attribute 'join'
This is the complete error traceback:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/moederborden/
Django Version: 1.7
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'pcbuilder')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/robin/Work/School/A-Pc/Website/pcbuilder/views.py" in moederborden
728. moederbordenlijst = compatibility(request,moederbordenlijst)
File "/home/robin/Work/School/A-Pc/Website/pcbuilder/compatibility.py" in compatibility
6. objectlijst_filtered = moederbordenComp(request,objectlijst)
File "/home/robin/Work/School/A-Pc/Website/pcbuilder/compatibility.py" in moederbordenComp
34. print objectlijst_filtered
File "/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/queryset.py" in __repr__
58. self._populate_cache()
File "/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/queryset.py" in _populate_cache
93. self._result_cache.append(self.next())
File "/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/base.py" in next
1137. raw_doc = self._cursor.next()
File "/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/base.py" in _cursor
1182. self._cursor_obj = self._collection.find(self._query,
File "/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/base.py" in _query
1215. self._mongo_query = self._query_obj.to_query(self._document)
File "/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/visitor.py" in to_query
92. query = query.accept(QueryCompilerVisitor(document))
File "/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/visitor.py" in accept
157. return visitor.visit_query(self)
File "/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/visitor.py" in visit_query
80. return transform.query(self.document, **query.query)
File "/usr/local/lib/python2.7/dist-packages/mongoengine/queryset/transform.py" in query
87. value = field.prepare_query_value(op, value)
File "/usr/local/lib/python2.7/dist-packages/mongoengine/fields.py" in prepare_query_value
106. value = re.escape(value)
File "/usr/lib/python2.7/re.py" in escape
214. return pattern[:0].join(s)
Exception Type: AttributeError at /moederborden/
Exception Value: 'list' object has no attribute 'join'
Any help would be greatly appreciated. I've used the filter method in several other parts of the project without any issues so I'm at a loss.
Thanks in advance.

Error in import FloatField, using django-import-export

I am using django-import-export for import csv file. I have a FloatField in my model :
models.py
purchase_price = models.FloatField(null=True, blank=True)
When I import csv file with blank value, it throws an error :
ValueError at /admin/csv_imp/book/process_import/
could not convert string to float:
Request Method: POST
Request URL: http://localhost:8000/admin/csv_imp/book/process_import/
Django Version: 1.7.1
Exception Type: ValueError
Exception Value:
could not convert string to float:
Exception Location: /home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py in get_prep_value, line 1550
Python Executable: /home/bgdev/Desktop/virtualenvs/django17/bin/python
Python Version: 2.7.6
Python Path:
['/home/bgdev/Desktop/virtualenvs/django17/test_pro',
'/home/bgdev/Desktop/virtualenvs/django17/src/admin-bootstrap',
'/home/bgdev/Desktop/virtualenvs/django17/lib/python2.7',
'/home/bgdev/Desktop/virtualenvs/django17/lib/python2.7/plat-i386-linux-gnu',
'/home/bgdev/Desktop/virtualenvs/django17/lib/python2.7/lib-tk',
'/home/bgdev/Desktop/virtualenvs/django17/lib/python2.7/lib-old',
'/home/bgdev/Desktop/virtualenvs/django17/lib/python2.7/lib-dynload',
'/usr/lib/python2.7',
'/usr/lib/python2.7/plat-i386-linux-gnu',
'/usr/lib/python2.7/lib-tk',
'/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages',
'/home/bgdev/Desktop/virtualenvs/django17/lib/python2.7/site-packages']
Server time: Tue, 9 Dec 2014 05:33:36 +0000
Environment:
Request Method: POST
Request URL: http://localhost:8000/admin/csv_imp/book/process_import/
Django Version: 1.7.1
Python Version: 2.7.6
Installed Applications:
('bootstrap_admin',
'import_export',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'csv_imp')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
105. response = view_func(request, *args, **kwargs)
File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
52. response = view_func(request, *args, **kwargs)
File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
204. return view(request, *args, **kwargs)
File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/import_export/admin.py" in process_import
130. raise_errors=True)
File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/import_export/resources.py" in import_data
359. six.reraise(*sys.exc_info())
File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/import_export/resources.py" in import_data
345. self.save_instance(instance, real_dry_run)
File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/import_export/resources.py" in save_instance
163. instance.save()
File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/db/models/base.py" in save
591. force_update=force_update, update_fields=update_fields)
File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/db/models/base.py" in save_base
619. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/db/models/base.py" in _save_table
681. forced_update)
File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/db/models/base.py" in _do_update
725. return filtered._update(values) > 0
File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/db/models/query.py" in _update
600. return query.get_compiler(self.db).execute_sql(CURSOR)
File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
1004. cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
775. sql, params = self.as_sql()
File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in as_sql
969. val = field.get_db_prep_save(val, connection=self.connection)
File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_db_prep_save
627. prepared=False)
File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_db_prep_value
619. value = self.get_prep_value(value)
File "/home/bgdev/Desktop/virtualenvs/django17/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py" in get_prep_value
1550. return float(value)
Exception Type: ValueError at /admin/csv_imp/book/process_import/
Exception Value: could not convert string to float:
I found similar issue with and IntegerField. Being the last column in the csv file, it turns out that I was introducing an extra End of Line character in the dataset, even if the field had an empty value. Removing the EOL character from every line before adding it to the dataset fixed the issue:
for line in file.readlines():
line = line.rstrip('\r\n')
dataset.append(tuple(item for item in line.split(',')))
This link will show you what null and blank do: differentiate null=True, blank=True in django
If you can arrange your csv file to have all of the null values at the end, you can just remove the commas. That might work. Or perhaps create a temporary model that assigns those fields to a Charfield. Then copy appropriate fields to your final model
This is untested:
update <table> set <field> = 0 where id in (select id from <table> where <field> = "");
Fill in your table name and the field name as appropriate. You can run this from your sql command line program

Categories