Как я могу экспортировать данные из базы данных Django, чтобы улучшить их так, как я классифицировал ниже? - PullRequest
0 голосов
/ 09 мая 2020

В models.py у меня есть эта кодовая база:

class Person(models.Model):
sex_choices = (
    ('Male', 'Male'),
    ('Female', 'Female')
)
martial_choices = (
    ('Single', 'Single'),
    ('Married', 'Married'),
    ('Divorce', 'Divorce'),
    ('Widowed', 'Widowed')
)
education_choices = (
    ('Below Matric', 'Below Matric'),
    ('Matriculate', 'Matriculate'),
    ('12 Pass', '12 Pass'),
    ('Graduate', 'Graduate'),
    ('PG', 'PG'),
    ('Ph.D', 'Ph.D'),
)
occupation_choices = (
    ('Student', 'Student'),
    ('Govt. Employee', 'Govt. Employee'),
    ('Unemployed', 'Unemployed'),
    ('Self-Employed', 'Self-Employed'),
    ('Cultivator', 'Cultivator'),
    ('Pvt. Employee', 'Pvt. Employee'),
    ('Minial Job', 'Minial Job'),
    ('Others', 'Others')
)
income_choices = (
    ('Nill', 'Nill'),
    ('Below ₹10k', 'Below ₹10k'),
    ('₹10k to ₹25k', '₹10k to ₹25k'),
    ('₹25k to ₹50k', '₹25k to ₹50k'),
    ('Above ₹50k', 'Above ₹50k')
)


name = models.CharField(max_length=200)
sex = models.CharField(choices=sex_choices, max_length=50)
martial_status = models.CharField(choices=martial_choices, max_length=50)
age = models.IntegerField()
education_level = models.CharField(choices=education_choices, max_length=50)
occupation_status = models.CharField(choices=occupation_choices, max_length=50)
income = models.CharField(choices=income_choices, max_length=50)

"Government Documents:"
registered_voter = models.BooleanField(default=False)
procured_certificate_available = models.BooleanField(default=False)
aadhar_card_available = models.BooleanField(default=False)
bank_account_available = models.BooleanField(default=False)


"Details of Education Qualification"


def __str__(self):
    return self.name


class DetailsOfEducationQualification(models.Model): #for age 3-23
type_choice = (
    ("Government", "Government"),
    ("Private", "Private"),
    ("Anganwadi Center", "Anganwadi Center"),
)
education_proximity_choice = (
    ("0-5", '0-5km'),
    ('5-10', '5-10km'),
    ('10+', 'Above 10km'),
    ('Outside the state', 'Outside the state'),
)

person = models.ForeignKey(Person, on_delete=models.CASCADE)
course_class = models.CharField(max_length=50, blank=True)
type_of_education_sector = models.CharField(choices=type_choice, max_length=50, blank=True)
education_facility_proximity = models.CharField(choices=education_proximity_choice, max_length=50, blank=True)
satisfaction_of_study = models.BooleanField(default=False, blank=True)
scholarship_received = models.BooleanField(default=False, blank=True)
"Specify the scholarship Left"
drop_out = models.BooleanField(default=False, blank=True)
"Reason Of DropOut Left"
drop_out_reason = models.CharField(max_length=1000, blank=True)

class ScholarshipNames(models.Model):
     details = models.ForeignKey(Person, on_delete=models.CASCADE)
     name = models.CharField(max_length=200, blank=True)

В файле admin.py у меня есть этот код:

from .models import (
Person,
DetailsOfEducationQualification, ScholarshipNames)
from tabular_export.admin import export_to_csv_action, export_to_excel_action


class DetailsOfEducationQualificationInline(admin.TabularInline):
    model = DetailsOfEducationQualification
    extra = 0
class ScholarshipNamesInline(admin.TabularInline):
    model = ScholarshipNames
    extra = 0
class PersonAdmin(admin.ModelAdmin):
fieldsets = [
    (
        'Personal Information', {
            'fields':[
                'name', 'sex', 'age', 'martial_status', 'education_level', 'occupation_status', 'income',
                ]
            }
        ), 
        (
            'Government Documents', {
                'classes':[
                    'collapse',
                ],
                'fields':[
                    ('registered_voter', 'procured_certificate_available', 'aadhar_card_available', 'bank_account_available',)
                    ]
                }
            ),
]
inlines = [
    DetailsOfEducationQualificationInline, ScholarshipNamesInline, EconomicActivityInGovernmentSectorInline, 
    EconomicActivityInPrivateSectorInline, EconomicActivityInAgricultureInline, AlliedFarmsInline, BusinessInline,
    EntreprenureshipInline,
]
actions = (export_to_csv_action, export_to_excel_action)

И при экспорте в формат Excel, Я получаю только атрибуты класса Person. Я хочу получить "Получить данные TabularInLine, а также данные класса Person" в формате Excel при экспорте ... Помогите мне !!!

...