Django Import Export data without ID

I tried to export data from Django to Excel, but every thing walk behind me, when I export a Excel File, Django Export only Ids :
id problem
all what I need is export Data and replace id with Column I choose.
I want to export Data (ex : name of rooms , name of Person

I tried to walk behind some tutorials like :
https://django-import-export.readthedocs.io/en/latest/api_widgets.html
and
https://stackoverflow.com/questions/59937531/django-call-id-expected-a-number-but-got-string.

In Django App, I have:
models.py



    from pyexpat import model
    from statistics import mode
    from django.db import models
    from django.contrib.auth.models import User
    from django.db.models.base import Model
    from django.forms import IntegerField



    class Post(models.Model):
        name = models.CharField(max_length=150)

        def __str__(self):
            return str(self.name)


    class Person(models.Model):
        post = models.ForeignKey(Post, on_delete=models.CASCADE)
        name = models.CharField(max_length=100)

        def __str__(self):
            return f"({self.name} is {self.post} "



    class Room(models.Model):
        name = models.CharField(max_length= 150 )

        def __str__(self):
            return str(self.name)


    class Classification(models.Model):
        name = models.ForeignKey(Person, on_delete=models.CASCADE)
        room = models.ForeignKey(Room, on_delete=models.CASCADE)
        date = models.DateField(auto_created=False)

        def __str__(self):
            return f"({self.name} Take {self.room}  at {self.date})"

admin.py


        from django.contrib import admin
        from import_export.admin import ImportExportModelAdmin
        from .models import *
        from import_export import fields, resources
        from import_export.widgets import ForeignKeyWidget


    @admin.register(Post, Room, Classification, Person)
    class TestappImportExport(ImportExportModelAdmin):
        pass

    class Classificationresource(resources.ModelResource):
        name = fields.Field(
            column_name='name',
            attribute='name',
            widget=ForeignKeyWidget(Person, 'name'))

        room = fields.Field(
            column_name = 'room',
            attribute = 'room',
            widget= ForeignKeyWidget(Room, 'name'))

        class Meta:
            fields = ('name', 'room')

even I tried to reverse class Classificationresource(resources.ModelResource) up of Classificationresource(resources.ModelResource), and I still get ids and not the name of Persons or Rooms.

So please help me to solve this problem, I tired to stay searching and I feel that I spend a time for that.

See the last example in the docs at Getting started — django-import-export 2.8.1.dev0 documentation

You should be able to use the ORM-style field references in the fields attribute of the Meta class in your ModelResources class.

For example, you should be able to use name__name to follow the foreign key relationship from the Classification object to the related Person object, to retrieve the name field of that Person object.

I tried this in admin.py:

from django.contrib import admin
from import_export.admin import ImportExportModelAdmin
from .models import *
from import_export import fields, resources
from import_export.widgets import ForeignKeyWidget


@admin.register(Post, Room, Classification, Person)
class TestappImportExport(ImportExportModelAdmin):
    pass

class Classificationresource(resources.ModelResource):
    name = fields.Field(
        column_name='name',
        attribute='name',
        widget=ForeignKeyWidget(Person, 'name')
    )

    room = fields.Field(
        column_name = 'room',
        attribute = 'room',
        widget= ForeignKeyWidget(Room, 'name'))

    
    class Meta:
        fields = ('id', 'name__name', 'room__name', 'date')

even this, it still export data with id

from django.contrib import admin
from import_export.admin import ImportExportModelAdmin
from .models import *
from import_export import fields, resources
from import_export.widgets import ForeignKeyWidget


@admin.register(Post, Room, Classification, Person)
class TestappImportExport(ImportExportModelAdmin):
    pass

class Classificationresource(resources.ModelResource):

    
    class Meta:
        model = Classification
        fields = ('id', 'name__name', 'room__name', 'date')

I get the solution with this :

class Classificationresource(resources.ModelResource):

    class Meta:
        model = Classification
        fields = ('id', 'name__name', 'room__name', 'date')
@admin.register(Classification) 
class TestappImportExport(ImportExportModelAdmin): 
    resource_class = Classificationresource
1 Like