Home > Software engineering >  Django Import Export data without ID
Django Import Export data without ID

Time:06-18

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 : enter image description here

I tried to walk behind some tutorials like : https://django-import-export.readthedocs.io/en/latest/api_widgets.html and 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.

CodePudding user response:

From the documentation:

When defining ModelResource fields it is possible to follow model relationships:

class BookResource(resources.ModelResource):

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

So I'd try this:

class Classificationresource(resources.ModelResource):

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

edit: It is also necessary to add

@admin.register(Classification) 
class TestappImportExport(ImportExportModelAdmin): 
    resource_class = Classificationresource
  • Related