def to_internal_value(self, attachment): if 'item_id' in attachment and 'type_id' in attachment: type = self.Meta.type_model.objects.get(pk=attachment['type_id']) cls = type.right.model_class() obj = get_by_identifier(cls.objects, attachment['item_id']) attachment[self.Meta.item_id_field] = obj.pk attachment[self.Meta.item_ct_field] = type.right.pk del attachment['item_id'] return super(RelationshipSerializer, self).to_internal_value(attachment)
def to_internal_value(self, attachment): if 'item_id' in attachment and 'type_id' in attachment: type = self.Meta.type_model.objects.get(pk=attachment['type_id']) cls = type.right.model_class() obj = get_by_identifier(cls.objects, attachment['item_id']) attachment[self.Meta.item_id_field] = obj.pk attachment[self.Meta.item_ct_field] = type.right.pk del attachment['item_id'] return super(RelationshipSerializer, self).to_internal_value( attachment )
def filter_queryset(self, request, queryset, view): ctype = get_ct(view.model) filter = {} for key, val in list(view.kwargs.items()) + list(request.GET.items()): if not key.startswith('related_'): continue if isinstance(val, list): val = val[0] for pct in get_related_parents(ctype): if key == 'related_' + pct.identifier: pclass = pct.model_class() parent = get_by_identifier(pclass.objects, val) objs = view.model.objects.filter_by_related(parent) filter['pk__in'] = objs.values_list('pk', flat=True) return queryset.filter(**filter)