Skip to content

Commit

Permalink
FIX #17 - Exception when attempting access something on the related_n…
Browse files Browse the repository at this point in the history
…ame.

Thanks @CelC for report, @geeknam for initial patch.
  • Loading branch information
bmihelac committed Mar 1, 2013
1 parent b25e6f9 commit f869b99
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
6 changes: 6 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
Change Log
===========

0.1.4 (non released)
====================

* Handle OneToOneField, FIX #17 - Exception when attempting access something
on the related_name.

0.1.3
=====

Expand Down
5 changes: 4 additions & 1 deletion import_export/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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', ):
Expand Down
8 changes: 8 additions & 0 deletions tests/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
22 changes: 21 additions & 1 deletion tests/core/tests/resources_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
skipUnlessDBFeature,
)
from django.utils.html import strip_tags
from django.contrib.auth.models import User

import tablib

Expand All @@ -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):
Expand Down Expand Up @@ -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):

Expand Down

0 comments on commit f869b99

Please sign in to comment.