Skip to content

Commit

Permalink
Enable Relationship.attr functionality across reads and writes
Browse files Browse the repository at this point in the history
  • Loading branch information
brosner committed Nov 21, 2016
1 parent 7979840 commit caead73
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pinax/api/relationships.py
Expand Up @@ -5,10 +5,10 @@

class Relationship(object):

def __init__(self, api_type, collection=False):
def __init__(self, api_type, collection=False, attr=None):
self.api_type = api_type
self.collection = collection
self.attr = None
self.attr = attr

def resource_class(self):
return registry.get(self.api_type)
7 changes: 4 additions & 3 deletions pinax/api/resource.py
Expand Up @@ -162,16 +162,17 @@ def get_attr(self, attr):
return resolve_value(value)

def get_relationship(self, related_name, rel):
attr = rel.attr if rel.attr is not None else related_name
if rel.collection:
iterator = getattr(self.obj, related_name)
iterator = getattr(self.obj, attr)
if not isinstance(iterator, collections.Iterable):
if not hasattr(iterator, "all"):
raise TypeError("Relationship {} must be iterable or QuerySet".format(related_name))
else:
iterator = iterator.all()
return iterator
else:
return getattr(self.obj, related_name)
return getattr(self.obj, attr)

def set_attr(self, attr, value):
if hasattr(self, attr.obj_attr):
Expand All @@ -191,7 +192,7 @@ def set_relationship(self, related_name, rel, value):
related_name, value["data"]["id"]
)
})
setattr(self.obj, related_name, o)
setattr(self.obj, f.name, o)
else:
# A collection can be:
# * ManyToManyField
Expand Down

0 comments on commit caead73

Please sign in to comment.