Repository: django-import-export/django-import-export

Issue:
Exception when attempting access something on the related_name.
I have something like this:

```
class Profile(models.Model):
    user = models.OneToOneField('auth.User')

class Entry(models.Model):
    user = models.ForeignKey('accounts.Profile')

class EntryResource(resources.ModelResource):
    class Meta:
        model = Entry
        fields = ('user__profile',)
```

Which causes: 'RelatedObject' object has no attribute 'get_internal_type' I've resorted to custom fields with dehydrate.



Discussion:
Thanks for report, I would check this soon.

I had the same issue, fixed it in my fork :)

Sent from Galaxy Nexus
On 21 Feb 2013 18:11, "Bojan Mihelac" notifications@github.com wrote:

> Thanks for report, I would check this soon.
> 
> —
> Reply to this email directly or view it on GitHubhttps://github.com/bmihelac/django-import-export/issues/17#issuecomment-13883894.

Great, is this a commit that fixes this issue:

https://github.com/bmihelac/django-import-export/commit/79afdfd571dc173a71bb6ad4680832d6cdd956c3

Way back, there only is only one if statement added. Sorry cant help much,
Im on my phone now.

Sent from Galaxy Nexus
On 21 Feb 2013 18:16, "Bojan Mihelac" notifications@github.com wrote:

> Great, is this a commit that fixes this issue:
> 
> 79afdfdhttps://github.com/bmihelac/django-import-export/commit/79afdfd571dc173a71bb6ad4680832d6cdd956c3
> 
> —
> Reply to this email directly or view it on GitHubhttps://github.com/bmihelac/django-import-export/issues/17#issuecomment-13884072.

no problem. If you have time to see which was the statement for this during today or tomorrow, I will be glad to avoid doing any work :)

This commit: https://github.com/geeknam/django-import-export/commit/907d08369cec61b6c290cb08358c91e6e6b2975f


Resolving patch:
diff --git a/import_export/resources.py b/import_export/resources.py
--- a/import_export/resources.py
+++ b/import_export/resources.py
@@ -9,6 +9,7 @@
 from django.utils.safestring import mark_safe
 from django.utils.datastructures import SortedDict
 from django.db import transaction
+from django.db.models.related import RelatedObject
 from django.conf import settings
 
 from .results import Error, Result, RowResult
@@ -367,6 +368,8 @@ def __new__(cls, name, bases, attrs):
                         f = model._meta.get_field_by_name(attr)[0]
                         model = f.rel.to
                     f = model._meta.get_field_by_name(attrs[-1])[0]
+                    if isinstance(f, RelatedObject):
+                        f = f.field
 
                     FieldWidget = new_class.widget_from_django_field(f)
                     widget_kwargs = new_class.widget_kwargs_for_field(field_name)
@@ -396,7 +399,7 @@ def widget_from_django_field(cls, f, default=widgets.Widget):
         if internal_type in ('ManyToManyField', ):
             result = functools.partial(widgets.ManyToManyWidget,
                     model=f.rel.to)
-        if internal_type in ('ForeignKey', ):
+        if internal_type in ('ForeignKey', 'OneToOneField', ):
             result = functools.partial(widgets.ForeignKeyWidget,
                     model=f.rel.to)
         if internal_type in ('DecimalField', ):


Test patch:
diff --git a/tests/core/models.py b/tests/core/models.py
--- a/tests/core/models.py
+++ b/tests/core/models.py
@@ -28,3 +28,11 @@ class Book(models.Model):
 
     def __unicode__(self):
         return self.name
+
+
+class Profile(models.Model):
+    user = models.OneToOneField('auth.User')
+
+
+class Entry(models.Model):
+    user = models.ForeignKey('auth.User')
diff --git a/tests/core/tests/resources_tests.py b/tests/core/tests/resources_tests.py
--- a/tests/core/tests/resources_tests.py
+++ b/tests/core/tests/resources_tests.py
@@ -7,6 +7,7 @@
         skipUnlessDBFeature,
         )
 from django.utils.html import strip_tags
+from django.contrib.auth.models import User
 
 import tablib
 
@@ -16,7 +17,7 @@
 from import_export import results
 from import_export.instance_loaders import ModelInstanceLoader
 
-from ..models import Book, Author, Category
+from ..models import Book, Author, Category, Entry, Profile
 
 
 class MyResource(resources.Resource):
@@ -265,6 +266,25 @@ def test_m2m_import(self):
         book = Book.objects.get(name='FooBook')
         self.assertIn(cat1, book.categories.all())
 
+    def test_related_one_to_one(self):
+        # issue #17 - Exception when attempting access something on the
+        # related_name
+
+        user = User.objects.create(username='foo')
+        profile = Profile.objects.create(user=user)
+        Entry.objects.create(user=user)
+        Entry.objects.create(user=User.objects.create(username='bar'))
+
+        class EntryResource(resources.ModelResource):
+            class Meta:
+                model = Entry
+                fields = ('user__profile',)
+
+        resource = EntryResource()
+        dataset = resource.export(Entry.objects.all())
+        self.assertEqual(dataset.dict[0]['user__profile'], profile.pk)
+        self.assertEqual(dataset.dict[1]['user__profile'], '')
+
 
 class ModelResourceTransactionTest(TransactionTestCase):