Django import export,get返回了多个

问题描述

在使用django-import-export时,请帮助我找到此错误解决方案 这是我的代码

模型:

class ChartOfAccounts(models.Model):
code = models.CharField('code plan comptable',max_length=20,unique=True)
name = models.CharField('plan comptable',max_length=100)

def __str__(self):
    return self.code


class Company(models.Model):
    code = models.CharField('code société',unique=True)
    name = models.CharField('société',max_length=100)
    addr = models.CharField('adresse',max_length=100,blank=True,null=True)
    chart_of_accounts = models.ForeignKey(ChartOfAccounts,on_delete=models.CASCADE,verbose_name='code plan comptable')
    
    def __str__(self):
    return self.code


class GLAccount(models.Model):
    class Meta:
        unique_together = (('code','chart_of_accounts'),)

    code = models.CharField('code compte comptable',max_length=10)
    chart_of_accounts = models.ForeignKey(ChartOfAccounts,verbose_name='code plan comptable')
    name = models.CharField('compte comptable',help_text='text descriptif du compte comptable')
    
    def __str__(self):
        return f'{self.code},{self.chart_of_accounts}'


class CompanyAccount(models.Model):
    company = models.ForeignKey(Company,verbose_name='code société',on_delete=models.CASCADE)
    gl_account = models.ForeignKey(GLAccount,verbose_name='compte comptable',on_delete=models.CASCADE)

资源:

class CompanyAccountResource(ModelResource):
    class Meta:
        model = models.CompanyAccount
        fields = ('company','gl_account',)
        exclude = ('id',)
        import_id_fields = ('company',)
        skip_unchanged = False
        report_skipped = False

    # fields
    company = Field(
        column_name=Meta.model._Meta.get_field('company').verbose_name,attribute='company',widget=ForeignKeyWidget(models.Company,field='code')
    )
    gl_account = Field(
        column_name=Meta.model._Meta.get_field('gl_account').verbose_name,attribute='gl_account',widget=ForeignKeyWidget(models.GLAccount,field='code')
    )

    def get_export_order(self):
        export_fields = ['company',]
        return export_fields

我的数据是:

Company model data here

ChatOfAccounts model data here

GLAccount model data here

CompanyAccountResource Excel canvas to import data

问题:

一个GLAccount代码可能会出现在两个会计科目表中,每个会计科目与一个公司相关,并且当尝试将数据从excel导入CompanyAccountResource时,会出现以下错误

    Line number: 1 - get() returned more than one GLAccount -- it returned 2!
S001,600000
Traceback (most recent call last):
File "C:\Users\Leo\PycharmProjects\Django\DjangoSnippets\venv\lib\site-packages\import_export\resources.py",line 639,in import_row
instance,new = self.get_or_init_instance(instance_loader,row)
File "C:\Users\Leo\PycharmProjects\Django\DjangoSnippets\venv\lib\site-packages\import_export\resources.py",line 334,in get_or_init_instance
instance = self.get_instance(instance_loader,line 327,in get_instance
return instance_loader.get_instance(row)
File "C:\Users\Leo\PycharmProjects\Django\DjangoSnippets\venv\lib\site-packages\import_export\instance_loaders.py",line 29,in get_instance
params[field.attribute] = field.clean(row)
File "C:\Users\Leo\PycharmProjects\Django\DjangoSnippets\venv\lib\site-packages\import_export\fields.py",line 66,in clean
value = self.widget.clean(value,row=data)
File "C:\Users\Leo\PycharmProjects\Django\DjangoSnippets\venv\lib\site-packages\import_export\widgets.py",line 396,in clean
return self.get_queryset(value,row,*args,**kwargs).get(**{self.field: val})
File "C:\Users\Leo\PycharmProjects\Django\DjangoSnippets\venv\lib\site-packages\django\db\models\query.py",line 433,in get
raise self.model.MultipleObjectsReturned(
app1.models.GLAccount.MultipleObjectsReturned: get() returned more than one GLAccount -- it returned 2!

解决方法

发生错误是因为您正在定义don't uniquely identify an objectCnt<- c(9940000126,9940000188,9940000406,9940000992,9940001017,9940001288,9940001833,9940002276,9940002629) FY12_April <- c(0,0) FY12_August <- c(0,.343545,0) FY12_December <- c(0,0) FY12_February <- c(0,0) FY12_January <- c(0,0.98557,0.41949703) FY12_July <- c(0,-1.211583915,0) FY12_June <- c(-1.47268885,-0.80164469,0) SamData <- data.frame(Cnt,FY12_April,FY12_August,FY12_December,FY12_February,FY12_January,FY12_July,FY12_June) ProcessData <- SamData %>% mutate(Count = apply(select(.,FY12_April:FY12_June),1,function(x) sum(x!=0,na.rm=TRUE))) %>% mutate(FirstInst = colnames(select(.,FY12_April:FY12_June))[apply(select(.,function(x)which(x!=0)[1])]) %>% mutate(FirstInstAmt = apply(select(.,function(x)which(x!=0,arr.ind = TRUE,useNames = TRUE)[1]))

导入工作流使用

import_id_fields来标识要更新的现有模型实例。在您的情况下,“ company”和“ gl_account”的组合可标识import_id_fields中的多行。

如果您需要导入逻辑来更新现有实例,则必须找到一种方法来唯一标识要更新的行。