Monday, July 5, 2021

DJango import-export overriding obj_import fails to import

In the below code, it is absolutely necessary to call self.import_field(field, obj, data) otherwise the import operation does not work. 

Also. The statements below also to be specified else gives crash 


   if isinstance(field.widget, widgets.ManyToManyWidget):

                continue



def import_obj(self, obj, data, dry_run):

        """

        Traverses every field in this Resource and calls

        :meth:`~import_export.resources.Resource.import_field`.

        """

        for field in self.get_fields():

            if isinstance(field.widget, widgets.ManyToManyWidget):

                continue


            # find specific `field_name`

            # param of `data` is OrderDict

            if field.column_name == 'username':

                data.update({'username': 'Hello %s' % data.get('username')})

            # checkout the changed object

            print(obj)


            self.import_field(field, obj, data)



References:

https://stackoverflow.com/questions/41962594/django-import-export-generate-username-before-user-csv-upload

No comments:

Post a Comment