Esempio n. 1
0
def nestedmodelformset_factory(model=None, from_dbfield=None, parent_model=None,
        base_form=NestedModelFormset, fields=None, exclude=None, readonly=None):
    model = model if model else get_related_class(from_dbfield, parent_model)
    field_names = fields or model._meta.fields.keys()
    meta_attrs = {'model':model, 'fields':field_names, 'exclude':exclude, 'readonly':readonly}
    meta = type('Meta', (object,), meta_attrs)

    return type('%sFormset'%model.__name__, (base_form,), {'Meta':meta})
Esempio n. 2
0
    def __init__(self, items, parent, field):
        self.parent = parent
        self.field = field
        self.related_class = get_related_class(field, parent) if field.related else None
        self._items = []

        for item in (items or []):
            self.append(item, new=False)

        self._old_hash = self.make_hash()
Esempio n. 3
0
    def clean_value(self, inst, value):
        # TODO: supporting multiple database routing by the method "allow_relation"
        if isinstance(value, dict):
            if self.related:
                cls = get_related_class(self, inst)
            elif not value.get('class', None):
                raise InvalidRelationValue('The relation "%s" has not a class.'%value)
            else:
                if isinstance(value['class'], basestring):
                    try:
                        cls = import_anything(value['class'])
                    except ImportError:
                        raise InvalidRelationValue('The class for relation "%s" was not found.'%value)
                else:
                    cls = value['class']

            # Loads the related object
            try:
                value = cls.query().get(pk=value['pk'])
            except cls.DoesNotExist: # FIXME we have to find another way to treat this
                value['error'] = 'Object not found'
        return value
Esempio n. 4
0
    def get_related_queryset(self, obj):
        if not self.related:
            return []

        return get_related_class(self, obj).query()