django-import-export / django-import-export

Django application and library for importing and exporting data with admin integration.
https://django-import-export.readthedocs.org/en/latest/
BSD 2-Clause "Simplified" License
3.02k stars 798 forks source link

Export from admin panel #1953

Open chkur opened 1 week ago

chkur commented 1 week ago

Describe the bug When export with admin - get no foreign keys in exported file. It worked earlier. FK example:

class OrderRequestResource(resources.ModelResource):
    email = Field(
        attribute="client__email",
        column_name="Email",
    )

To Reproduce Steps to reproduce the behavior:

  1. Go to admin
  2. Click on Export button
  3. Watch the file - there are no foreign key fields.

Versions (please complete the following information):

Expected behavior Export with admin button must export all fields described for export, including foreignkeys

Additional context Export in admin works in 3.3.9, installed that version.

matthewhegarty commented 1 week ago

Thanks for raising. It looks like a valid bug, because when I try in the example app, I can see 'Author Name' in the output, but it doesn't appear in the exported csv file.

class BookResource(ModelResource):
    name = Field(attribute="author__name", column_name="Author Name")

    class Meta:
        model = Book

image

'Author Name' not in the output:

image

The workaround is to define the field (referenced by attribute) in the fields list:

    class Meta:
        fields = ("author__name", )
        model = Book

Then the field will appear in the export.

Docs for reference.

Note if I reference by declared field name, then it appears in the output list, but not in the csv:

        fields = ("name", )

image

(csv is empty file)

matthewhegarty commented 1 week ago

This was fixed in #1903 (which will be released in v4.2.0). If you use the main branch the issue should be fixed. I added a test in #1955.

matthewhegarty commented 1 week ago

Not actually fixed. The test added in #1955 shows that the wrong field is exported (the book name is exported, not the author name).

matthewhegarty commented 1 week ago

This is the default export output in v3:

Author Name,id,author,author_email,imported,published,published_time,price,added,categories
George R. R. Martin,11,11,martin@got.com,0,1996-08-01,21:00:00,25.00,,1
matthewhegarty commented 1 week ago

The POST parameters don't identify the Author Name field

image

matthewhegarty commented 1 week ago

Workarounds

  1. name the field author__name:
    author__name = Field(attribute='author__name', column_name='Author Name' )
  1. Declare the field in fields:
        fields = ("author__name",)

    (although this writes the column name as author__name)

Declare the field as 'Author Name' and it will export ok:

        fields = ("Author Name",)