<bdo id='Vqhh7'></bdo><ul id='Vqhh7'></ul>
    1. <legend id='Vqhh7'><style id='Vqhh7'><dir id='Vqhh7'><q id='Vqhh7'></q></dir></style></legend>
    2. <small id='Vqhh7'></small><noframes id='Vqhh7'>

      <tfoot id='Vqhh7'></tfoot>

    3. <i id='Vqhh7'><tr id='Vqhh7'><dt id='Vqhh7'><q id='Vqhh7'><span id='Vqhh7'><b id='Vqhh7'><form id='Vqhh7'><ins id='Vqhh7'></ins><ul id='Vqhh7'></ul><sub id='Vqhh7'></sub></form><legend id='Vqhh7'></legend><bdo id='Vqhh7'><pre id='Vqhh7'><center id='Vqhh7'></center></pre></bdo></b><th id='Vqhh7'></th></span></q></dt></tr></i><div id='Vqhh7'><tfoot id='Vqhh7'></tfoot><dl id='Vqhh7'><fieldset id='Vqhh7'></fieldset></dl></div>

      在 django-import-export 中处理foreignKeys的导入

      Dealing with import of foreignKeys in django-import-export(在 django-import-export 中处理foreignKeys的导入)
    4. <i id='dzaLl'><tr id='dzaLl'><dt id='dzaLl'><q id='dzaLl'><span id='dzaLl'><b id='dzaLl'><form id='dzaLl'><ins id='dzaLl'></ins><ul id='dzaLl'></ul><sub id='dzaLl'></sub></form><legend id='dzaLl'></legend><bdo id='dzaLl'><pre id='dzaLl'><center id='dzaLl'></center></pre></bdo></b><th id='dzaLl'></th></span></q></dt></tr></i><div id='dzaLl'><tfoot id='dzaLl'></tfoot><dl id='dzaLl'><fieldset id='dzaLl'></fieldset></dl></div>

          <tbody id='dzaLl'></tbody>

        1. <legend id='dzaLl'><style id='dzaLl'><dir id='dzaLl'><q id='dzaLl'></q></dir></style></legend>

            <small id='dzaLl'></small><noframes id='dzaLl'>

              <tfoot id='dzaLl'></tfoot>
                <bdo id='dzaLl'></bdo><ul id='dzaLl'></ul>
                本文介绍了在 django-import-export 中处理foreignKeys的导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我不明白 django-import-export 模块如何处理 ForeignKeys.这是一个简单的例子:模型.py

                I don't understand how django-import-export module deals with ForeignKeys. Here is a simple exemple : models.py

                class TFamilies(models.Model):
                    id_fam = models.AutoField(primary_key=True, unique=True)
                    name_fam = models.CharField(max_length=1024, blank=True,verbose_name='Famille')
                
                
                class TGenus(models.Model):
                    id_genus = models.AutoField(primary_key=True, unique=True)
                    name_genus = models.CharField(max_length=1024,verbose_name='nom de genre')
                    id_fam = models.ForeignKey(TFamilies, null=True, db_column='id_fam', blank=True, verbose_name='Famille')
                

                我想允许人们添加与家庭相关的属!只有 name_genus 和 name_fam 的 CSV/XLS...(并且 id 留空).

                I would like to allow people adding genus with family associated ! A CSV/XLS with only name_genus and name_fam... (and id left blank).

                家族大部分时间已经存在于 DB 中,Django 只需要找到正确的 id 号...

                Family already exist in DB most of the time, Django juste have to find the right id number...

                admin.py

                class TGenusResource(resources.ModelResource):
                
                    name_fam = fields.Field(widget=widgets.ForeignKeyWidget(TFamilies, 'name_fam'))
                
                    class Meta:
                        model = TGenus
                        import_id_fields = ['id_genus']
                
                
                class TGenusAdmin(ImportExportActionModelAdmin):
                    form = TGenusAdminForm
                    resource_class = TGenusResource
                    pass
                

                此配置导致导入界面出错:

                This configuration lead to error in import interface :

                Line number: 1 - 'NoneType' object has no attribute 'name_fam'
                Traceback (most recent call last):
                File "/....../lib/python2.7/site-packages/import_export/resources.py", line 348, in import_data
                row_result.object_repr = force_text(instance)
                File "......./lib/python2.7/site-packages/django/utils/encoding.py", line 85, in force_text
                s = six.text_type(s)
                AttributeError: 'NoneType' object has no attribute 'name_fam'
                

                我不明白...我也尝试过回答:django-import-export 外键字段的资源定义?有点像那里:使用django迁移django中的外键-进出口

                I don't understand... I also tried answer there : django-import-export resource definition for foreignkey field? and sort of like there : Foreign Key in django migration using django-import-export

                我必须使用 before_import 找到自己的匹配?

                Do I have to use before_import to find myself the match ?

                推荐答案

                我发现你必须找到匹配的自己!更改 tablib 数据集中的值是不可能的,因此您必须对条目进行更改并将它们放回新行,然后删除旧行.

                I figured out that you have to find yourself the match ! It's impossible to alterate values in a tablib dataset so you have to take entries make changes and put them back in a new line then erase the old one.

                我的 excel 模板包含列 id_genus(空)、name_genusid_fam 由家庭名称填充!

                My excel template contains columns id_genus (empty), name_genus and id_fam filled by the name of the family !

                对于登陆这里的任何人,我都会发布我的方式:

                For anyone who landed here I post my way :

                def before_import(self, dataset, dry_run):
                        """
                        Make standard corrections to the dataset before displaying to user
                        """
                
                        i = 0
                        last = dataset.height - 1
                
                        # for all lines, search for id of family given and add a new line at the bottom with it then delete the first one
                        while i <= last:
                            # Check if the Genus exist in DB
                            if (TGenus.objects.filter(name_genus=dataset.get_col(2)[0].capitalize())):
                                id_genus = TGenus.objects.filter(name_genus=dataset.get_col(2)[0].capitalize())[0].id_genus
                            else :
                                id_genus = ''
                            # Check if the family exists in DB
                            try:
                                TFamilies.objects.get(name_fam=dataset.get_col(2)[0].capitalize())
                            except TFamilies.DoesNotExist:
                                raise Exception("Family not in DB !")                   
                            except TFamilies.MultipleObjectsReturned:
                                pass
                
                            # use of "filter" instead of "get" to prevent duplicate values, select the first one in all cases
                            dataset.rpush((id_genus,
                                dataset.get_col(1)[0],
                                TFamilies.objects.filter(name_fam=dataset.get_col(2)[0].capitalize())[0].id_fam))
                            dataset.lpop()
                            i = i + 1
                

                非系统管理员可以使用我的 django 管理员,因此,他们可以复制不在数据库中的属或族...如果有人有更好地处理错误的想法,我想阅读它!另外,我想在预览中保留家人的名字,而不仅仅是她的身份证……如果你知道怎么做,我已经发布了另一个关于此的问题:是否可以在 django import-export 中自定义预览模板?

                My django admin can be used by non sys-admin so, they can and duplicate genus or families that aren't in DB... If someone have an idea to deal with errors better, I would like to read it ! Also, I would like to keep the name of the family in the preview, not only her id... If you know how, I have posted another question about that : Is-it possible to customize the template of preview in django import-export?

                这篇关于在 django-import-export 中处理foreignKeys的导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                How to extend Python class init(如何扩展 Python 类初始化)
                What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                Initialize list with same bool value(使用相同的布尔值初始化列表)
                setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

                    <bdo id='iHO4J'></bdo><ul id='iHO4J'></ul>
                    <i id='iHO4J'><tr id='iHO4J'><dt id='iHO4J'><q id='iHO4J'><span id='iHO4J'><b id='iHO4J'><form id='iHO4J'><ins id='iHO4J'></ins><ul id='iHO4J'></ul><sub id='iHO4J'></sub></form><legend id='iHO4J'></legend><bdo id='iHO4J'><pre id='iHO4J'><center id='iHO4J'></center></pre></bdo></b><th id='iHO4J'></th></span></q></dt></tr></i><div id='iHO4J'><tfoot id='iHO4J'></tfoot><dl id='iHO4J'><fieldset id='iHO4J'></fieldset></dl></div>
                        • <legend id='iHO4J'><style id='iHO4J'><dir id='iHO4J'><q id='iHO4J'></q></dir></style></legend>

                          <small id='iHO4J'></small><noframes id='iHO4J'>

                        • <tfoot id='iHO4J'></tfoot>
                            <tbody id='iHO4J'></tbody>