def field_from_native(self, data, files, field_name, into): if self.partial and not data.get(field_name, None): return enter_data = data[field_name] if isinstance(enter_data, models.Account): cls = ContentType.objects.get(app_label = 'accounts', model = enter_data.__class__.__name__) into[field_name] = enter_data if self.content_type: into['%s_type' % field_name] = cls into['%s_object_id' % field_name] = enter_data.id return enter_data if isinstance(enter_data, (str, unicode)): data = models.filter_accounts(display_name = enter_data) if not data: raise serializers.ValidationError(u"用户%s不存在。" % enter_data) else: data = data[0] enter_data = {'type': data['account_type'], 'id': data['id']} cls = ContentType.objects.get(app_label = 'accounts', model = enter_data['type']) try: obj = cls.model_class().objects.get(pk = enter_data['id']) except cls.model_class().DoesNotExist: raise serializers.ValidationError(u"用户%s不存在。" % enter_data['display_name']) into[field_name] = obj if self.content_type: into['%s_type' % field_name] = cls into['%s_object_id' % field_name] = obj.id return obj
def companies(request): data = models.filter_accounts(account_type=['company', 'fundcompany', 'bank']) _ = [] for i in data: i.pop('class') _.append(i) return Response(_)
def field_from_native(self, data, files, field_name, into): enter_data = data[field_name] if isinstance(enter_data, (str, unicode)): data = models.filter_accounts(display_name = enter_data) if not data: raise ValidationError("account") into[field_name] = None return None else: data = data[0] enter_data = {'type': data['account_type'], 'id': data['id']} cls = ContentType.objects.get(app_label = 'accounts', model = enter_data['type']) try: obj = cls.model_class().objects.get(pk = enter_data['id']) except cls.model_class().DoesNotExist: raise Http404 into['%s_type' % field_name] = cls into['%s_object_id' % field_name] = enter_data['id'] into[field_name] = obj return obj
def validate_display_name(self, attrs, source): display_name = attrs['display_name'] if models.filter_accounts(display_name = display_name): raise serializers.ValidationError(u"用户名%s已存在。" % display_name) return attrs