Created
April 19, 2014 05:05
-
-
Save tiliv/11074668 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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