question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GenericForeignKey import/export

See original GitHub issue

How do you manage Generic Relations using django-import-export ?

A GenericForeignKey combines two informations to generate a relation: a content_type and an object_id.

Since each field has his own logic in django-import-export, I was thinking of using before_import_row or before_save_instance to define the combination of the above two fields.

Has anyone tried building a GenericForeignKeyWidget ?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:12

github_iconTop GitHub Comments

2reactions
wreeeckscommented, Apr 9, 2019

@msmaromi For a generic foreign key field called address on my model Dude, I just have this field in my ModelResource:

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']

So in the CSV file (or whatever format you’re using), import/export will use whatever you specify in the address column like:

Address.objects.get(name=name)

Using ForeignKeyWidget to get the GenericRelation didn’t work.

2reactions
johnfraneycommented, Oct 8, 2017

I was able to import/export a generic foreign key using the built-in ForeignKey widget.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The contenttypes framework | Django documentation
from django.contrib.contenttypes.fields import GenericForeignKey from ... Give your model a GenericForeignKey , and pass it the names of the two fields ...
Read more >
Django Import/Export in a table where foreign key is a string ...
I have explored a lot of examples of Django export/import. When I try importing in a table with a foreign key with datatype...
Read more >
Django Import Export Tricks & Tips - Dev Genius
Tips for using import-export with its special features. Importing Exporting of data in Django becomes easier with its special library Django Import Export...
Read more >
Creating models for diverse content | Django 2 by Example
from django.contrib.contenttypes.fields import GenericForeignKey. Copy. Then, add the following code to the end of the file: class Content(models.Model):
Read more >
Django QuerySet to CSV Files & Datasets - Blog Post
analytics.models.py from django.conf import settings from ... models from django.contrib.contenttypes.fields import GenericForeignKey from ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found