Giter VIP home page Giter VIP logo

django-import-export / django-import-export Goto Github PK

View Code? Open in Web Editor NEW
2.9K 65.0 775.0 3.31 MB

Django application and library for importing and exporting data with admin integration.

Home Page: https://django-import-export.readthedocs.org/en/latest/

License: BSD 2-Clause "Simplified" License

Python 96.57% HTML 2.32% Shell 0.33% JavaScript 0.34% Makefile 0.44%
python django import-export csv xls json

django-import-export's People

Contributors

ad-m avatar adamchainz avatar adibo avatar andrewgy8 avatar ayumukasuga avatar beruic avatar bmihelac avatar busterbeans avatar claudep avatar cuchac avatar geeknam avatar inodb avatar jarekwg avatar jdufresne avatar jnns avatar manelclos avatar matthewhegarty avatar mgrdcm avatar mikhailsidorov avatar petrdlouhy avatar petrmifek avatar pidelport avatar qris avatar rhunwicks avatar schemacs avatar shaggyfrog avatar thauk-copperleaf avatar tomyam1-personal avatar trik avatar will-copperleaf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-import-export's Issues

question:UnicodeDecodeError at /admin/blog/author/import/

Sir,thanks a lot to develop this great repository.
I have little problem to solve , as below
the code:

#in models.py
class Author(models.Model):
    author = models.CharField('作者', max_length=50)
    title = models.CharField('标题', max_length=150)
    qualification = models.ForeignKey(Qualification)
    mark = models.ManyToManyField(Mark)
    blog = models.TextField('博客内容')
    time = models.DateField('写作日期')

    def __unicode__(self):
        return unicode(self.author)

    class Meta:
        ordering = ['time']
#in admin.py
class AuthorAdmin(ImportExportModelAdmin, admin.ModelAdmin):
    search_fields = ('author', 'title', 'mark', 'blog')
    list_display = ('author', 'title', 'time')

When I export the data,
1
but the csv file appear to be the retortion,
2
when I modified the second row author data and import,

3
it cause the error,
4

how I it be smoothly modified and import?thanks.
Allenlee

Transaction error after import

First, great work on django-import-export. It's a very helpful library.

Setup:
Django 1.5.4
Postgres 9.2.4.1 (13)

Here's what's going on:

  1. Fresh database
  2. Add object via admin. Success!
  3. Import objects onto same model via django-import-export with format csv. No errors, objects imported correctly.
  4. Add object via admin. Failure with error: "current transaction is aborted, commands ignored until end of transaction block". The same error occurs for model.objects.create(...) calls via external scripts.

What I've tried:

  • Adding, removing, changing order of TransactionMiddleware
  • Trying both django admin gui and a standalone script for import
  • Adding, removing 'OPTIONS': {'autocommit': True,} to Database settings
  • All these "fix after error" suggestions from stackoverflow
  • Manually saving imported objects via django admin before trying to add more
  • Adding, removing global setting 'IMPORT_EXPORT_USE_TRANSACTIONS = True' per docs

I've run out of options and information on how to approach this problem.

If this has nothing to do with django-import-export, feel free to promptly close this issue. Just want to provide a data point of what's happening and perhaps learn from others who have experienced it.

How best to handle FKs when imports do not contain PKs?

Hey,

This is just a question to start with but something more may come out of it..

An example situation

We have a simple CSV for a Car model. In it, it contains a field which relates to a Driver model but not by an integer PK - instead it contains a string which is the surname of the driver. For this example we will assume a unique surname == a unique driver. So we have:

Our Models

from django.db import models

class Car(models.Model):
    name = models.CharField(max_length=20)
    driver = models.ForeignKey('taxi.Driver')

class Driver(models.Model):
    surname = models.CharField(max_length=20, unique=True)

Our django-import-export Resource

class CatResource(resources.ModelResource):

    class Meta:
        model = Car

Our CSV File

name,driver
Mercedes A Class,Jim Bob
BMW M5,Hugo Smith
Lexus R,Sally Archer

What we want to happen

  • We want to dry run the data to make sure it's all OK.
  • On iterating through the CSV, we want to something to do a lookup from the driver field value to the Driver model so that can retrieve it's PK and save the Car instance. If the Driver instance does not exist, we would like to create it.

Possible ways to go about this

  1. We write a widget to do the lookup. The widget takes two extra positional args in the form of "model" & "lookup_field" - the model the lookup is to occur on and the field on which to do it respectively. In the widget's clean method it takes the string value it finds, does the lookup on the appropriate model and creates the instance if not found. It returns the instance found/created instead of the original string.

_Good: it gets the job done.
_Bad
: I don't think ORM stuff really belongs here but more importantly: it doesn't respect the dry_run parameter as it doesn't know about it. Therefore even on a failed import we still have numerous Driver instances created.

  1. We write the logic to do this in the Resource's before_save_instance method hook. It would look something like this:
def before_save_instance(self, instance, dry_run):
    try:
        driver = Driver.objects.get(title=x)
    except Driver.DoesNotExist:
        # The lookup failed. We need to create a driver object but we
        # should only do so if this is not a dry run.
        driver = Driver(title=x)
        if not dry_run:
            driver.save()
    instance.driver = driver

_Good: this seems a better place to put this kind of logic; we also have access to the dry_run state here so it's easy to just not commit your instances.
_Bad
: this is actually impossible as the method has no access to the row data. x is what we need but we can't get at it.

Provided I'm not missing another way to nicely solve this (please do say if I am!) then I propose the following fairly simple to implement solution:

before_save_instance gets called from save_instance which gets passed the instance and whether it's a dry run or not. To get row data in before_save_instance so that we can do more in the hook, we'd simply have to pass the row data into save_instance and then pass it on.

e.g from Resource.import_data

from:
self.save_instance(instance, real_dry_run)
to:
self.save_instance(instance, row, real_dry_run)

I realise this may not be something you want to support, in which case that's absolutely fine but I thought I'd bring this issue to light as when you're importing data it's more likely than not that you'd be linking FKs on some kind of natural key as opposed to the primary.

Thank you for any help you can give on this! I'm happy to write a pull request if you like the solution, or even if you propose another one.

cheers,
Darian

Import of customized content trouble

Hi Bojan,

having an issue how to import content of a custom content of a field previously exported with dehydrate_<fieldname> method.

For example I do export value of Price model which is related to (ForeignKey of) Product model in own format:
Trying to implement hydrate_<fieldname> as an inverse counterpart to dehydrate method for import conversions.

from product.models import Product, Price
from import_export import resources, fields, widgets

class ProductResource(resources.ModelResource):
    price       = fields.Field( widget=widgets.ForeignKeyWidget(Price) )

   class Meta:
        model   = Product
        fields  = ('id', 'sku', 'name', 'price', 'categories', ...)
        widgets = {}
        import_id_fields = ['sku']

    def import_field(self, field, obj, data):
        meth = getattr(self, 'hydrate_{}'.format(field.column_name), None)
        if meth is not None:
            # store custom field format back into a database
            data = meth(obj, data)
        elif field.attribute and field.column_name in data:
            field.save(obj, data)

    def hydrate_price(self, product, row):
        if product.price_set.filter(expires__isnull=True, quantity=1).exists():
            product.price_set.update(price=Decimal(row['price']), quantity=1)
        else:
            product.price_set.create(price=Decimal(row['price']), quantity=1)

Above attempt however fails, because import_field is called before a new Product is instantiated (ie. it hasn't been yet populated into a database so not yet assigned an id).

Line number: 20 - IntegrityError('product_price.product_id may not be NULL',)

Traceback (most recent call last):
File "/home/dunric/Projects/xstore/lib/python2.7/site-packages/import_export/resources.py", line 282, in import_data
self.import_obj(instance, row)
File "/home/dunric/Projects/xstore/lib/python2.7/site-packages/import_export/resources.py", line 185, in import_obj
self.import_field(field, obj, data)
File "/home/dunric/Projects/xstore/store/../store/localsite/models.py", line 49, in import_field
data = meth(obj, data)
File "/home/dunric/Projects/xstore/store/../store/localsite/models.py", line 88, in hydrate_price
product.price_set.create(price=Decimal(row['price']), quantity=1)

How can be done customized import if content of an imported field is not in a simple relation "import_value <-> database_value" ?

Altering fields in Tastypie-like pattern?

I'm wondering if there's a way to manipulate or annotate the content of the field. Similar to the way Tastypie does it (by dehydrating fields)?

Example:

class BookResource(resources.ModelResource):
    full_title = fields.Field(column_name='full_title')    

    class Meta:
        model = Book

    def dehydrate_full_title(self, obj):
        return '%s by %s' % (obj.name, obj.author.name)

replace widget's validation by Django field's validation

Hello o/,

I'm was looking for a way to validate field's choices during the import and
I was wondering if you would agree to modify the validation/clean process
done by the widgets to use Django Fields instead.

I'm working on a patch/pull request that do that and I wanted to know
if there is some limitation or use cases I have not think of.

Cheers!

Overriding queryset on export() fails when passed queryset has zero elements

I'm passing in a custom queryset to the export function, but if the queryset I pass in returns no elements, then the if statement resets it to get the default queryset... not what is intended, I'm sure.

314: def export(self, queryset=None):
315: if not queryset:
316: queryset = self.get_queryset()

Line 315 should be
if queryset is None:

Import : how to add a select form for a foreignkey

Hello,

I would like to import a model where i ahem a foreign key. All imported rows will be linked to one entry of this foreign key.
I can add a column with the same number for each row, but i prefer to add a select box in the import form.
How can i do that ?

Thx

Exceptions during introspection

I'm getting exceptions when it's trying to do the introspection for some of the model fields (m2m and foreignkey fields) even when i exclude them or when i write my own dehydrate fields. For instance this one for a tags m2m field:

'TaggableManager' object has no attribute 'get_internal_type' resources.py in widget_from_django_field, line 395

I still get this error when i set up the resource like this:

class StoryResource(resources.ModelResource):
tags = fields.Field()

class Meta:
    model = Story
    fields = ('title', 'content', 'year', 'tags')

def dehydrate_tags(self, story):
    return "%s" % (story.tags.all(), )

Or when i leave out the tags field completely:

class StoryResource(resources.ModelResource):

    class Meta:
        model = Story
        fields = ('title', 'content', 'year')

It seems the introspection is done on all fields no matter what you set here? I'm using the master from github not the version on pypi btw (which has the same issue)

export filter not working

Thank you very much for this app! It really helps me alot.

Only a problem, that applied filters when export don't work.

To repair it - please change
export.html, line 14

from
action="{% url opts|admin_urlname:"export" %}" method="POST">
to
action="{{ request.get_full_path }}" method="POST">
'django.core.context_processors.request' must be activated.

I also patched importing and exporting csv for changing delimiter to ';' from ',' , because excel really don't like comma separated csv.

---------------import_export changes

patch for excel usage to change standard csv separators from ',' to ';'

in admin.py:

def import_action

changes:

if not input_format.is_binary() and self.from_encoding:

data = unicode(data, self.from_encoding).encode('utf-8')

if import_formats[int(form.cleaned_data['input_format'])]==base_formats.CSV:

data=data.replace(';',',')

dataset = input_format.create_dataset(data)

def export_action

changes:

data = resource_class().export(queryset)

if formats[int(form.cleaned_data['file_format'])]==base_formats.CSV:

data_string=file_format.export_data(data)

datastring=data_string.replace(',',';')

response = HttpResponse(

datastring,

mimetype='application/octet-stream',

)

------------------------------------

xlxs import / export

Hi,

In base_formats.py I found below.
But I can;t see this in Django Admin as option to import/export xlsx files

Do I need extra libs to make this working ?

class XLSX(TextFormat):
    TABLIB_MODULE = 'tablib.formats._xlsx'

Relating column to fields in model

I'm trying to get a field in the excel like sortorder to be imported into the id in the model and things more complicated as concatenate two fields with text into a field in the model, is this possible?

Resize very large files for confirm view

Hi!

As I will upload very large files, for confirm view I would like resize it, and read just the first 20-30 entries which will be displayed in the Preview. I know I have to monkey patch import_action, but which would be the approach from cutting the file and still have support for all provided formats? I don't want to write a case for each one separately.

After the user confirmation I will assign the processes to a worker.

Regards,
Dacian

Export button not visible when no add permission

When add permission is disabled for a model the admin change list doesn't display an Export button.

My solution was to use this template:

{% extends "admin/import_export/change_list_export.html" %}
{% load i18n %}

{% block object-tools %}
    {% if not has_add_permission %}
        <ul class="grp-object-tools">
            {% block object-tools-items %}
                <li><a href="export/{{ cl.get_query_string }}" class="export_link">{% trans "Export" %}</a></li>
            {% endblock %}
        </ul>
    {% else %}
    {{ block.super }}
    {% endif %}
{% endblock %}

Note: I'm using django-grappelli

a fix to base_format.py

def get_extension(self):
return self.get_format().extensions[0]

'extensions' is a typo, it should be 'extentions'

(I couldn't export through the admin otherwise)

Installing via pip does not bundle admin templates

Installed via pip:
pip install django-import-export

There were no admin templates (even the directory) in site-packages inside virtualenv.
Admin integration does not work when installing package via pip - Template not found is raised.

Export does not respect filters

Imported from #22:

button "export" must export all list items that is shown after pressing some filters, etc. For me it was exporting ALL items, without filters. Its because GET parameters of filter wasn't send to request in

Add screenshots to documentation

It took me a while to discover what the admin change list was supposed to look like when I was troubleshooting #38. A screenshot in the documentation would have allowed me to discover the missing Export button much quicker.

Also, screenshots tend to make documentation look more appealing.

Hooking admin EntryLog to after_save_instance

I would like to add something like this to an overridden Resource.after_save_instance function to keep LogEntry consistent:

class ExampleResource(resources.ModelResource):
    def after_save_instance(self, instance, dry_run):
        from django.contrib.admin.models import LogEntry, ADDITION
        LogEntry.objects.log_action(
            user_id=request.user.pk,
            content_type_id=ContentType.objects.geggt_for_model(object).pk,
            object_id=object.pk,
            object_repr=force_text(object),
            action_flag=ADDITION
        )

Problem is that I don't have access to request from there. Don't know if it is a good idea to pass it on from ImportMixin or that you want to keep request logic out of resources.py? Another way would be to override process_import in ImportMixin and use the result returned by resouce.import_data to update LogEntry. Any thoughts on that?

Import from .xls - ManyToManyWidget AttributeError

An AttributeError error occurs when importing from a .xls file that has a single integer entry in a field representing a ManyToMany relationship.

The issue seems to be that the single integer entry is read in as a float. Then when
ids = value.split(",")
is called on line 153 of import_export/widgets.py the error occurs.

A possible solution could be to add the following two lines above line 153:
if isinstance(value, float):
value = str(int(value))

Feature request: use verbose_name instead of field name for column names

When your exporting data from your database as xls or csv it's usually for human reading so it would be nice if the column names would be the verbose_name of the model fields. i.e. "Company name" instead of company_name for a field: company_name = models.CharField(_('Company name'))

As far as i can tell this is not currently possible, even just passing a display_name value to a (custom) field for a resource would be a nice fix...

Column names with spaces

Can't use column names with spaces - at least, as far as I could tell.

If this is indeed the case, and I'm not just being stupid, the fix is in resources.py at line 299.

    method = getattr(self, 'dehydrate_%s' % field.column_name, None)

becomes

    method = getattr(self, 'dehydrate_%s' % field.column_name.replace(' ', '_'), None)

Current use cases should be unaffected by that change.

Django's opts.get_all_field_names() do not have related fields

This is a mysterious one. I still can't find out what's causing it.

class ModelA(models.Model):
    modelb = models.ForeignKey('ModelB')

class ModelB(models.Model):
   field1 = models.CharField(max_length=100)

class ModelC(models.Model):
    modelb = models.ForeignKey('ModelB')


# Filter by related field
ModelB.objects.filter(modelc__field=value)  # this is ok if the ModelResource is not defined

# Create resource for ModelA
class ModelAResource(resources.ModelResource):
    class Meta:
        models = Model
        fields = [
            'field1', 'modelb__field'
        ]

ModelB.objects.filter(modelc__field=value)  # this is not working anymore

Somehow Django's opts.get_all_field_names() does not contain 'modelc' field.
I'll attempt to write a test on a branch that fails this testcase

Question : is code used to follow relationships needed for imports ?

Hi,

I'm having a hard time understanding the code in resources.py, especially these lines. I think I got my head around it, and I just want to confirm something.

Is the whole bunch of lines from 431-455 just dedicated to export ? What happens if ResourceOptions.fields contains a field that folllows a relationship (ex joint_venture__company__name, and we import some data ? Would the name attribute (of company) get updated for each row ?


Also (don't read if you're in a hurry):

I recently started to integrate django-import-export in a big object management application that I'm writing for a client, and to make import/export functionality available in my generic views (with support for filtered exports as it's already available for the admin, dynamically sorted exports, and on-the-fly import resource definition). In order to do that, I need to understand django-import-export's code, and I started to refactor it a bit. I'll be happy to submit my changes, however, sadly I made a lot of coding style changes, and I know that many developers really don't like that. Sorry :(. Anyway, here is a list of things I did, it'd be very kind of you to me tell if you see anything in that list that you really dont want to merge :

  • Line-length: I try to stick with the standard of 79 chars in each line.
  • Blank lines: I use many blank lines in long functions, makes the line length bigger but it think it's better for readibility
  • Long statement folding: I tend to fold long lines a lot, and I usually put each argument in its line when indent level is high, or argument names are long
  • Commenting: I comment a lot, especially when refactoring. I'll try to clean them up at the end
  • Docstrings: I start docstrings just after the opening triple-quote, and always leave an empty line at the end (PEP-style). I try to document every function, especially when refactoring.
  • Code splitting: I split code in small functions, maybe too much. Also, when using Django internals (such as _meta), I tend to access them only with functions defined in a separate file, so that if there's any change upstream (as _meta retro compatibility is not guaranteed at all)
  • Functional builtins: I prefer using for item in filter(lambda x: test, list): <code> rather than for item in list: if test: <code>, for example. Tell me if that annoys you, I can easily revert this.
  • Generators: I also use them frequently, especially when dealing with big datasets.
  • Var names: I hate f ! I prefer having longer, explicit names than short, easy-to-type names.
  • Function names: For example, I changed the name of Resource.get_export_headers, as is it not specific to export (it is called for each line when importing data)

Additional customization hooks

There are some hooks in Resource to allow easy customization: before_save_instance, etc.

I want to take an extract file and use it to create some parent models as part of the same import process. The best way for me to do this, is to call the import_data method for the other ModelResources immediately before I start processing the rows (so that I am already inside the transaction management, if required). At the moment, I have to copy and paste the entire import_data method into my Resource: it would be better if we had something like:

        if use_transactions is True:
            # when transactions are used we want to create/update/delete object
            # as transaction will be rolled back if dry_run is set
            real_dry_run = False
            transaction.enter_transaction_management()
            transaction.managed(True)
        else:
            real_dry_run = dry_run

        instance_loader = self._meta.instance_loader_class(self, dataset)

        self.before_import(dataset, real_dry_run)

        for row in dataset.dict:

Similarly, some of my customizations work by overriding import_obj, but this can more difficult than necessary because we don't know whether we are in a dry run or not.

This could be solved by adding dry_run to the method signature of import_obj and passing it when you call it from import_data.

Would those changes be acceptable? If they would, I'll prepare a pull request.

During import, table id will not be auto-incremented

Running on:

  • Django-1.6
  • django-import-export-0.1.5
  • postgresql.x86_64-9.2.5-1
  • tablib-0.9.11
  • python-2.7.5-9

I have successfully imported data from a csv file.

When I try to add more data manually, I receive the error:

duplicate key value violates unique constraint "mytable_pkey"
DETAIL:  Key (id)=(1) already exists.

From psql:

select max(id) from mytable;
  75

SELECT nextval('mytable_id_seq');
  2

Does django-import-export support the table's id auto-incrementing?

Bugfix release (0.1.4?)

I spent some time debugging and troubleshooting the export filtering functionality on a 0.1.3 installation, and eventually discovered that the cause was fixed some time ago in #23.

0.1.3 was released half a year ago. Could a new point release be made, to save people from having to debug and run a git checkout to get this fix?

import eav fileds

I have not only static fieds in my model, but some additional eav fields
http://en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80%93value_model .

I am getting and setting them by

my_model_instance.eav.my_field

#or even as simle fields

my_model_instance.my_field

and they are automatically saved when

 my_model_instance.save()

See Advanced data manipulation example in documentation, this should help you export non model fields.

I see only how it can help me to export...

I need something as "hydrate" for import :(((

Any advices?


regarding export for excel, you can use tsv or xls format, that could be easiest.

No. To xls you can only export, but can't import. Also, excel 2010 don't know how to open tsv from the box. Only from import master with option "tab separator". I will die before someone in office learn how to do it (((

question:the alias of the import fields

1
If I want use the verbose name from models:'标签''证书' to be aliasies of the import fields,How can I do it?

class Mark(models.Model):
    mark = models.CharField('标签', max_length=50)
    license = models.CharField('证书', max_length=50)

thanks

Exception when attempting access something on the related_name.

I have something like this:

class Profile(models.Model):
    user = models.OneToOneField('auth.User')

class Entry(models.Model):
    user = models.ForeignKey('accounts.Profile')

class EntryResource(resources.ModelResource):
    class Meta:
        model = Entry
        fields = ('user__profile',)

Which causes: 'RelatedObject' object has no attribute 'get_internal_type' I've resorted to custom fields with dehydrate.

Widget formatting for foreign key relations doesn't work

Assuming Author model has birthday field. The following widget formatting wouldn't work.

        class BookResource(resources.ModelResource):

            class Meta:
                model = Book
                fields = ('author__birthday',)
                widgets = {
                    'author__birthday': {'format': '%Y-%m-%d'},
                    # However this works
                    'birthday':  {'format': '%Y-%m-%d'},
                }

import_file_name form field can be use to access the filesystem

I've notice that your using the full path of the tempfile in the confirmation form.
While it would be hard to take advantage of this, it allows an attacker to try to read
and parse files by modifying the value of the hidden form field.

What I would propose is to use only the basename of the tempfile instead of the
full path. And then read it like this in the views:

import_file = open(os.path.join(tempfile.gettempdir(),                                         
os.path.basename(confirm_form.cleaned_data['import_file_name'])))

when import csv file ,KeyError: u'id' occured

i try to import some data form csv file, but errors occurred as follows:

Errors
Line number: 1 - u'id'
Traceback (most recent call last):
File "/home/zhijiasun/.virtualenvs/tutorial/local/lib/python2.7/site-packages/import_export/resources.py", line 317, in import_data
instance, new = self.get_or_init_instance(instance_loader, row)
File "/home/zhijiasun/.virtualenvs/tutorial/local/lib/python2.7/site-packages/import_export/resources.py", line 149, in get_or_init_instance
instance = self.get_instance(instance_loader, row)
KeyError: u'id'

I just modify the data of the csv file which exported by the app and then try to import it. But error happed.

Many to many fields and admin upload

Do many to many fields work via admin imports? I keep getting something like this:
ValueError('"<Accession: >" needs to have a value for field "accession" before this many-to-many relationship can be used.',)

It doesn't seem like it can if it's displaying the data to import before importing, as there's no way to get the primary key for the row since it doesn't have one yet?

Thanks,

Dean

How to setup resource definition for foreignkey field

Given these models::

class Company(models.Model):
    company_name = models.CharField(max_length=50, unique=True)
    ....

class Product(models.Model):
    company = models.ForeignKey(Company)
    product_name = models.CharField(max_length=100)
    ...

class Inventory(models.Model):
    product = models.ForeignKey(Product, null=True, unique=True)
    ...

Importing from an XLS into Inventory with company_name and product_name properly specified, that is the XLS file contains a single row specifing the company_name and product_name for a unique Product.

The product object can be found in Django/python by::

Product.objects.filter(company__company_name=company_name, product_name=product_name)

How should the Django-import-export resources.ModelResources be constructed to support import via the admin?

The Resource does not work in admin

In the django shell works wonders with the export resource, but when I do it from the admin export all the columns of the model and uses the PK in ForeignKeys.

# models.py
class Active(models.Model):
    tag = models.CharField(
        verbose_name=_('Tag'), max_length=255, blank=True, unique=True)
    serial_number = models.CharField(
        verbose_name=_('Serial number'), max_length=255, blank=True)
    mac_address = MACAddressField(
        verbose_name=_('Mac Address'), max_length=255, blank=True, null=True,
        unique=True)
    model = models.ForeignKey(
        Model, verbose_name=_('Manufacturer / Model'), null=True, blank=True)
    location = models.ForeignKey(
        Location, verbose_name=_('Location'), blank=True, null=True)
    ip_address = models.GenericIPAddressField(
        verbose_name=_('IP Address'), blank=True, null=True)
    client = models.ForeignKey(
        Client, verbose_name=_('Client'), related_name='actives',
        blank=True, null=True)
    state = models.ForeignKey(
        State, verbose_name=_('State'), null=True, blank=True)
    device = models.ForeignKey(
        Device, verbose_name=_('Device'), blank=True, null=True)
    observation = models.TextField(verbose_name=_('Observation'), blank=True)

    class Meta:
        ordering = ('tag',)
        verbose_name = _('active')
        verbose_name_plural = _('actives')

    def __unicode__(self):
        return self.tag
# resources.py
class ActiveResource(resources.ModelResource):

    class Meta:
        model = models.Active
        fields = ('model__name',)
        exclude = ('id',)
# admin.py
class ActiveAdmin(ImportExportModelAdmin):
    resouce_class = resources.ActiveResource

Ability to add multiple custom exports to ModelAdmin

I would like to be able to export different datasets. I currently have this for exporting a model and related data:

class GameAdmin(ExportMixin, admin.ModelAdmin):
    resource_class = GameResource
    # ...

However Games also have Levels, what I would like is having a mechanism to add multiple custom exports similar to how https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter works:

class GameAdmin(ExportMixin, admin.ModelAdmin):
    resource_class = [LevelsResource, GameResource]
    # ...

Which would let me export Level data.

Adding support for granting permissions

Hi guys, I am a beginner, but if you can help me, I am grateful.

I found nothing in the documentation about permissions...
It turns out I need the Export button is enabled in Django admin page only for a group of users.

I noticed that users who do not have permission to insert records, fail enchergar button to export the data.
However, in my case, some users who have access to app can insert records, but it would not be appropriate to allow them to be able to get this worksheet.

Finally, I wonder if anyone has a tip on how I should proceed to make just a user group can view the export in the admin page. Is there a possibility?

I also give the suggestion to leave the implementation of this feature in view of the use case mentioned above, because many times the user is allowed to enter any data on the page, and thus able to view the Impor/Export button.

PS : Sorry for the english , I'm learning :/

Support multiple languages for export

The columns of the xls are not correctly translated when exporting. I have a resource that should be lazy translated(english or dutch), but it shows the wrong language for the columns. I need a way to set the language to which to translate. I tried several ways, but it does not work.

Below is an example:
from django.utils.translation import ugettext_lazy as _

class MyResource(resources.ModelResource):
date = fields.Field(attribute='date', column_name=force_unicode(_('Date')))
...

def create_export_costs(export_file, exported_objects, lang):
my_resource = MyResource()
if lang:
translation.activate(lang)
dataset = my_resource.export(queryset=exported_objects)
export_file.write(dataset.xls)
return export_file

Cannot run tests - cannot import import_export.core.Importer

I was trying to write a test for django-import-export and when I try and run the tests I get:

  File "tests/core/tests/tests.py", line 5, in <module>
    from import_export.core import Importer
ImportError: No module named core

Is tests.py still valid?

importing xls file into modle with models.ForeignKey filed

Hi,

I'm using models from your test app

I have below code in my views.py

class BookResource(resources.ModelResource):

    class Meta:
        model = Book
        fields = ('id', 'author__name',)
        pass

When using export function everything is ok and I can revive correct author name (not id from model Authors)
But when trying to Import I can't import xls file with authors names.
So... no author name like in preview attached on bottom.
Not sure that I should config something extra to make this kind of import happen ?

in admin.py:

from django.contrib import admin

from import_export.admin import ImportExportMixin

from .models import Book, Category, Author
from .views import BookResource

class BookAdmin(ImportExportMixin, admin.ModelAdmin):
    list_filter = ['categories', 'author']
    resource_class = BookResource

Thanks,

Jakub

untitled

Suggestion:The encode conversion

Sir,I think over this question in django

UnicodeDecodeError at /admin/core/book/import/
'utf8' codec can't decode byte 0xc4 in position 0: invalid continuation byte

then I use vim set fileencoding,I found the csv standard of file exported from the django-import-export is UTF-8,I search it on internet,I also found that if i use utf-8 standard file to import,it's OK.

Maybe I use the OS in Chinese language so it cause the problem.Would you please modify the project:If the encode of import-file or export-file is not utf-8,first convert it to utf-8,then process other code?

I have write some code to fix the encode convertion:

import chardet

def convertEncoding(from_encode,to_encode,old_filepath,target_file):
    f1=file(old_filepath)
    content2=[]
    while True:
        line=f1.readline()
        content2.append(line.decode(from_encode).encode(to_encode))
        if len(line) ==0:
            break

    f1.close()
    f2=file(target_file,'w')
    f2.writelines(content2)
    f2.close()

convertFile = open('1234.csv','r')
data = convertFile.read()
convertFile.close()

convertEncoding(chardet.detect(data)['encoding'], "utf-8", "1234.csv", "1234_bak.csv")

I am a newbie , my code is not concise.would you please think about that and integrite the regular to the project?I very like this project,thanks for your reputation!

Add feature to export change list columns

It looks like only model fields are exported currently. I'm looking for an app that will also allow exporting the change list columns (sometimes they are aggregates or deep relations).

I can understand if this feature isn't desired since the resulting exported data couldn't be re-imported necessarily.

Adding a "related projects" section to the README and/or documentation with links to other similar projects (some with different goals probably) would be useful. One of the other admn export libraries probably provides this functionality.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.