fabiocaccamo / django-treenode

:deciduous_tree: probably the best abstract model/admin for your tree based stuff.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use django Import - Export option with this

manish2507 opened this issue · comments

Hi,

I am not able to use Import - Export feature of django along with this library. Can you please help me with the same

@manish2507 could you describe better your problem and steps to reproduce it, please?
Which python and django versions are you using?

@manish2507 could you describe better your problem and steps to reproduce it, please?
Which python and django versions are you using?

Hi, I am using Python-3.7.2 & Django-2.2.
I am trying to upload approx 10000 records using django-import-export because it is not possible by human to enter data one by one so if is there any option to do bulk upload using csv, xlxs etc.
If i can you the django-import-export with this plugin then it will be huge advantage.

@manish2507 and what is going wrong using django-import-export? I don't understand your problem.

@manish2507 and what is going wrong using django-import-export? I don't understand your problem.

Can you please provide me a sample to import the data using "django-import-export" to do bulk upload of data.

I guess this was a question asking to provide an example on how to use Django Import/Export together with Django-TreeNode. I'd like to recall more information on this question since I find myself a bit stuck in the process. I have a CSV with the following columns, and I'd like to import it, but I'm not sure how to do it without creating inconsistencies.

CSV file (first 7 rows, and with simplified data):

id name parent
1 Abc NULL
2 Cde 1
3 Efg 2
4 Hij 3
5 Klm 4
6 Nop 4

The solution I thought was to directly import the parent field to the tn_parent_id attribute, but I guess that will lead to inconsistencies.

Hope this clarifies a bit the issue.

@matiszz frankly I never did it, but you should:

  • Import the parent field to the tn_parent_id
  • Call YourModelClass.update_tree() (maybe this is unnecessary, but I'm not sure if the bulk import will trigger the signals or not)

If you test it (with and without calling update_tree, please let me know so I can update the documentation regarding the integration with django-import-export.

@fabiocaccamo thanks for your fast answer. You're right, there's no need to call update_tree() because the signal is triggered. Here is a sample code to work with django-import-export

class MyModelResource(resources.ModelResource):
    parent = Field(column_name='parent', attribute='tn_parent_id')

    def dehydrate_tn_parent_id(self, pathology):
        return pathology.tn_parent_id

    class Meta:
        model = MyModel
        import_id_fields = ('id',) # Maybe you don't need this
        skip_unchanged = True
        fields = ('id', 'name', 'parent', 'is_active')

@matiszz simpler than expected, thank you for the feedback!