Skip to content

Instantly share code, notes, and snippets.

@tiliv
Created April 19, 2014 05:05
Show Gist options
  • Save tiliv/11074668 to your computer and use it in GitHub Desktop.
Save tiliv/11074668 to your computer and use it in GitHub Desktop.
class RelatedCompaniesField(serializers.WritableField):
def __init__(self, *args, **kwargs):
self.company_type = kwargs.pop('company_type')
super(RelatedCompaniesField, self).__init__(*args, **kwargs)
def to_native(self, obj):
relationships = obj.relationships.filter_by_company_type(self.company_type)
ids = list(relationships.get_orgs(self.company_type, ids_only=True))
return ids
def from_native(self, data):
CompanyModel = COMPANY_MODELS_MAP[self.company_type]
return CompanyModel.objects.filter(id__in=data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment