def to_internal_value(self, data):
        data = super().to_internal_value(data)
        if not isinstance(data, dict):
            raise ValidationError('content_object has to be a dict.')
        if data.get('collection') is None or data.get('id') is None:
            raise ValidationError('collection and id has to be given.')
        try:
            data['id'] = int(data['id'])
        except TypeError:
            raise ValidationError('id has to be an int.')

        if not isinstance(data['collection'], str):
            raise ValidationError('collection has to be a string.')

        try:
            model = get_model_from_collection_string(data['collection'])
        except ValueError:
            raise ValidationError('Collection string "{}" is not valid.'.format(data['collection']))
        try:
            element = model.objects.get(pk=data['id'])
        except ObjectDoesNotExist:
            raise ValidationError('Id {} does not exist.'.format(data['id']))
        return element
Exemple #2
0
    def test_known_app(self):
        projector_model = collection.get_model_from_collection_string(
            'core/projector')

        self.assertEqual(projector_model, Projector)
Exemple #3
0
 def test_unknown_app(self):
     with self.assertRaises(ValueError):
         collection.get_model_from_collection_string('invalid/model')
    def test_known_app(self):
        projector_model = collection.get_model_from_collection_string('core/projector')

        self.assertEqual(projector_model, Projector)
 def test_unknown_app(self):
     with self.assertRaises(ValueError):
         collection.get_model_from_collection_string('invalid/model')