joshourisman / django-tablib

django-tablib is a helper library for Django that allows Django models to be used to generate tablib datasets with introspection of the fields on the models if no headers are provided. If headers are provided they can reference any attribute, fields, properties, or methods on the model.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ModelDataset evaluates queryset during import

beniwohli opened this issue · comments

Specifically, DatasetMetaclass evaluates the queryset twice:

if not opts.model and not opts.queryset:
    raise NoObjectsException("You must set a model or non-empty "
                             "queryset for each Dataset subclass")
if opts.queryset:
    queryset = opts.queryset
    model = queryset.model
    new_class.queryset = queryset
    new_class.model = model

This can lead to hard to debug problems, e.g. when the database tables don't exist yet. Instead, these if-conditions could specifically check if the queryset is not None:

if not opts.model and opts.queryset is None:
    raise NoObjectsException("You must set a model or non-empty "
                             "queryset for each Dataset subclass")
if opts.queryset is not None:
    queryset = opts.queryset
    model = queryset.model
    new_class.queryset = queryset
    new_class.model = model