fgmacedo / django-export-action Goto Github PK
View Code? Open in Web Editor NEWGeneric export action for Django's Admin
License: MIT License
Generic export action for Django's Admin
License: MIT License
Keep the fields order on reports and on the field selection page.
Export a models that uses UUIDField for primary key (and probably any other retular field).
My model:
class MyModel(models.Model):
id = models.UUIDField(
primary_key=True, max_length=37, unique=True,
default=uuid.uuid4, editable=False)
... # more stuff
In the admin export, selected all field, including 'id'.
Error:
'ValueError' object has no attribute 'message'
on last line in /python3.5/site-packages/export_action/report.py in build_sheet
:
try:
ws.append(row)
except ValueError as e:
ws.append([e.message])
Looking at a few frames above we see more relevant information:
In /python3.5/site-packages/openpyxl/worksheet/worksheet.py in append
else:
cell = Cell(self, row=row_idx, col_idx=col_idx, value=content)
Value of variable content
:
UUID('0dfafd80-baaf-4d95-b357-4804bda3d751')
Deselecting id
field in the export view removes the error (but of course, there are no IDs the export).
admin.py
The django.core.urlresolvers module is removed in favor of its new location, django.urls.
Installing via README and test export functionality
(venv) Michaels-MacBook-Pro:dn-sql-catalog msimons$ pip install django-export-action
Collecting django-export-action
Using cached django_export_action-0.1.1-py2.py3-none-any.whl
Collecting openpyxl (from django-export-action)
Collecting et-xmlfile (from openpyxl->django-export-action)
Collecting jdcal (from openpyxl->django-export-action)
Installing collected packages: et-xmlfile, jdcal, openpyxl, django-export-action
Successfully installed django-export-action-0.1.1 et-xmlfile-1.0.1 jdcal-1.3 openpyxl-2.4.8
/Users/msimons/Code/dn-sql-catalog/venv/bin/python /Users/msimons/Code/dn-sql-catalog/manage.py runserver
Performing system checks...
Unhandled exception in thread started by <function wrapper at 0x10a67b758>
Traceback (most recent call last):
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
self.check(display_num_errors=True)
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/core/management/base.py", line 385, in check
include_deployment_checks=include_deployment_checks,
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/core/management/base.py", line 372, in _run_checks
return checks.run_checks(**kwargs)
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/core/checks/urls.py", line 14, in check_url_config
return check_resolver(resolver)
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/core/checks/urls.py", line 24, in check_resolver
for pattern in resolver.url_patterns:
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/urls/resolvers.py", line 310, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/urls/resolvers.py", line 303, in urlconf_module
return import_module(self.urlconf_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/msimons/Code/dn-sql-catalog/dn_sql_catalog/urls.py", line 23, in <module>
url(r'export_action/', include("export_action.urls", namespace="export_action")),
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 50, in include
urlconf_module = import_module(urlconf_module)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/export_action/urls.py", line 3, in <module>
from .views import AdminExport
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/export_action/views.py", line 11, in <module>
from . import report
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/export_action/report.py", line 20, in <module>
from six import BytesIO, text_type
ImportError: No module named six
running 'pip install six' and Django app goes back to running fine. But might want to include 'six' in the prerequisites?
Use the verbose version of field names on reports.
Navigating to the admin page for a model that includes 'id' column and choosing 'export selected' action proceeds to field selection dialog. However, attempting to export on a model that has a primary_key=True field with a name other than 'id' errors out on export.
model:
class Computer(models.Model):
comp_id = models.AutoField(db_column='CompId', primary_key=True)
name = models.CharField(db_column='CompName', max_length=50)
domain = models.CharField(db_column='Domain', max_length=10, choices=DOMAIN_CHOICES, null=True)
infrastructure = models.CharField(db_column='Infrastructure', max_length=10,
choices=INFRASTRUCTURE_CHOICES, blank=True, null=True)
site = models.ForeignKey(DNUnits, db_column='site_id', related_name='computer_site', blank=True, null=True)
machine_type = models.CharField(db_column='MachineType', max_length=1,
choices=MACHINE_TYPE_CHOICES, blank=True, null=True)
OS = models.CharField(db_column='OS', max_length=50, blank=True, null=True)
CPU_count = models.IntegerField(db_column='CPUCount', blank=True, null=True)
class Meta:
unique_together = ('name', 'domain',)
ordering = ('name',)
def __unicode__(self):
return self.name + '.' + get_display(self.domain, DOMAIN_CHOICES)
Traceback:
Traceback (most recent call last):
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/core/handlers/exception.py", line 39, in inner
response = get_response(request)
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/contrib/admin/options.py", line 544, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 211, in inner
return view(request, *args, **kwargs)
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/utils/decorators.py", line 67, in _wrapper
return bound_func(*args, **kwargs)
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/utils/decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/utils/decorators.py", line 63, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/contrib/admin/options.py", line 1569, in changelist_view
response = self.response_action(request, queryset=cl.get_queryset(request))
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/contrib/admin/options.py", line 1305, in response_action
response = func(self, request, queryset)
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/export_action/admin.py", line 10, in export_selected_objects
selected = list(queryset.values_list('id', flat=True))
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/db/models/query.py", line 729, in values_list
clone = self._values(*fields)
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/db/models/query.py", line 712, in _values
query.add_fields(field_names, True)
File "/Users/msimons/Code/dn-sql-catalog/venv/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1644, in add_fields
"Choices are: %s" % (name, ", ".join(names)))
FieldError: Cannot resolve keyword 'id' into field. Choices are: CPU_count, OS, comp_id, domain, infrastructure, instance, machine_type, name, site, site_id
[28/Aug/2017 19:37:59] "POST /admin/dn_sql_catalog/computer/ HTTP/1.1" 500 129966
django-export-action==0.1.1
django==1.11.9
3.6.5
When I trying to export char field of the model into CSV format I got repr() of the string encoded to bytes instead of the string itself.
b'auth_token__key'
b'5e36616a85ff284433b8e3acb9f919513ac2a90a'
b'ad31300cd28ce281b9ea677c8ffb5e437f697134'
b'7e48a77e8261e115485807f45aab3edd5b47a7e7'
But instead of this, I expect the string value itself.
auth_token__key
5e36616a85ff284433b8e3acb9f919513ac2a90a
ad31300cd28ce281b9ea677c8ffb5e437f697134
7e48a77e8261e115485807f45aab3edd5b47a7e7
I trace execution to the export_action.report.list_to_csv_response
function. It contains this line
cw.writerow([force_text(s).encode(response.charset) for s in row])
If I remove the encode
method call, export works as expected.
def list_to_csv_response(data, title="report", header=None, widths=None):
""" Make 2D list into a csv response for download data.
"""
response = HttpResponse(content_type="text/csv; charset=UTF-8")
cw = csv.writer(response)
for row in chain([header] if header else [], data):
cw.writerow([force_text(s) for s in row])
return response
Maybe this is Python 2 & 3 compatibility issue. I can guess that on Python 2 CSV writer expect str
instead of Unicode. And on Python 3 it expects str
instead of bytes
.
Please, let me know if I can help somehow to resolve it.
Regards, Artem.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.