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

Support for GenericRelation #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion conduit/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.db.models.fields import FieldDoesNotExist
from django.db import models
from django.conf.urls import url
from django.contrib.contenttypes import generic
from django.contrib.contenttypes import fields, generic

from decimal import Decimal
from dateutil import parser
Expand Down Expand Up @@ -872,6 +872,16 @@ def _to_basic_type(self, obj, field):
if isinstance(field, models.ManyToManyField):
return eval(field.value_to_string(obj))

if isinstance(field, models.related.RelatedObject):
for virtual_field in self.Meta.model._meta.virtual_fields:
if virtual_field.name == 'content_object':
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This obviously needs to not be hardcoded. It seems like exposing this field might require it's own field where we specify the name of the attribute to look up. Otherwise, I'm wondering if we're better off just hiding this attribute from the response data.

related_obj = getattr(obj, virtual_field.name)
return field.field.value_to_string(related_obj)
return None

if isinstance(field, fields.GenericRelation):
return field.value_to_string(obj)

logger.info('Could not find field type match for {0}'.format(field))
return None

Expand Down