Skip to content

Commit

Permalink
Port fix from #1482
Browse files Browse the repository at this point in the history
  • Loading branch information
georgedorn committed Feb 25, 2019
1 parent 60c0c6f commit 6cb7da7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tastypie/fields.py
Expand Up @@ -740,7 +740,7 @@ def __init__(self, to, attribute, related_name=None, default=NOT_PROVIDED,

def contribute_to_class(self, cls, name):
super(ToOneField, self).contribute_to_class(cls, name)
if not self.related_name:
if not self.related_name and isinstance(self.attribute, six.string_types):
related_field = getattr(self._resource._meta.object_class, self.attribute, None)
if isinstance(related_field, ReverseOneToOneDescriptor):
# This is the case when we are writing to a reverse one to one field.
Expand Down
24 changes: 24 additions & 0 deletions tests/core/tests/resources.py
Expand Up @@ -1148,6 +1148,24 @@ class Meta:
authorization = Authorization()


class NoAttributeFKNoteResource(ModelResource):
editor = fields.ForeignKey(UserResource, None)

class Meta:
resource_name = 'noattrfknotes'
queryset = NoteWithEditor.objects.all()
authorization = Authorization()


class FunctionAttributeFKNoteResource(ModelResource):
editor = fields.ForeignKey(UserResource, lambda bundle: User.objects.first(), null=True)

class Meta:
resource_name = 'fnattrfknotes'
queryset = NoteWithEditor.objects.all()
authorization = Authorization()


class ThrottledNoteResource(NoteResource):
class Meta:
resource_name = 'throttlednotes'
Expand Down Expand Up @@ -1523,6 +1541,12 @@ def test_init(self):
resource_6 = CustomPageNoteResource()
self.assertEqual(resource_6._meta.paginator_class, CustomPaginator)

# FK fields with non-string attributes
resource_7 = NoAttributeFKNoteResource()
self.assertEqual(len(resource_7.fields), 9)
resource_8 = FunctionAttributeFKNoteResource()
self.assertEqual(len(resource_8.fields), 9)

def test_can_create(self):
resource_1 = NoteResource()
self.assertEqual(resource_1.can_create(), True)
Expand Down

0 comments on commit 6cb7da7

Please sign in to comment.