Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use to_field if specified in ForeignKey #441

Closed
wants to merge 1 commit into from

Conversation

manelclos
Copy link
Contributor

No description provided.

@manelclos
Copy link
Contributor Author

Fixes #204

@bmihelac
Copy link
Member

@manelclos is it any reason not to use

field = f.rel.field_name

instead of f. to_fields lines?

@bmihelac
Copy link
Member

I do not have any experience with using to_field, so comment above may be dumb

@manelclos
Copy link
Contributor Author

I started looking into this, but run out of time. On my TODO list.

We should start using related_field instead for django >= 1.9:
https://github.com/django/django/blob/901dc90db0d95ddcd9a7d9608c8158cb9dbd824c/django/db/models/fields/__init__.py#L244

@kellerkind
Copy link

I added @manelclos hint to export from to_field attribute to work in my code. Thanks for this!
Also to mention that resolving foreign model should be done via related_model since 1.8.

--- a/import_export/resources.py
+++ b/import_export/resources.py
@@ -676,8 +676,11 @@ class ModelResource(six.with_metaclass(ModelDeclarativeMetaclass, Resource)):
         result = functools.partial(widgets.ManyToManyWidget,
                                    model=f.rel.to)
     if internal_type in ('ForeignKey', 'OneToOneField', ):
+            field = 'pk'
+            if f.remote_field.field_name:
+                field = f.remote_field.field_name
         result = functools.partial(widgets.ForeignKeyWidget,
-                                       model=f.rel.to)
+                                       model=f.related_model, field=field)
     if internal_type in ('DecimalField', ):
         result = widgets.DecimalWidget
     if internal_type in ('DateTimeField', ):

@manelclos manelclos self-assigned this Nov 15, 2017
@manelclos
Copy link
Contributor Author

#672 fixes this the correct way

@manelclos manelclos closed this Nov 15, 2017
@bmihelac bmihelac deleted the fix-204-ForeignKeyWidget-to-field branch January 17, 2019 09:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants