简体   繁体   中英

django-import-export how to handle GenericRelations?

I'm using django-import-export module to export the record. However, I couldn't export the generic relations. I just want to get all the details of GenericRelation.

Found the snippet below in Github but it doesn't work.

class DudeResource(resources.ModelResource):
    address = fields.Field(
        column_name='address',
        attribute='address',
        widget=widgets.ForeignKeyWidget(Address, 'name'))  # use a unique field

    class Meta:
        model = Dude
        fields = ['address']

My Models

Company
|-- Name
|--- Address(Generic Relation)

Address
|--content_type
|--object_id
|--content_object
|--line_1
|--line_2
|--city
|--country

I just need to import/export line_1, line_2, city, and country. Can someone help me on this? Thanks!

Have you tried specifying the fields like this...

class DudeResource(resources.ModelResource):

class Meta:
    model = Dude
    fields = ['address__line_1', 'address__line_2', 'address__city',
              'address__line_1', 'address__country', ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM