def __new__(cls, name, bases, attrs): formfield_callback = attrs.pop("formfield_callback", None) try: parents = [b for b in bases if issubclass(b, SQLAModelForm)] except NameError: parents = None declared_fields = forms.forms.get_declared_fields(bases, attrs, False) new_class = super(SQLAModelFormMetaclass, cls).__new__(cls, name, bases, attrs) if not parents: return new_class if "media" not in attrs: new_class.media = forms.widgets.media_property(new_class) opts = new_class._meta = SQLAModelFormOptions(getattr(new_class, "Meta", None)) if opts.model: # If a model is defined, extract form fields from it. fields = fields_for_model(opts.model, opts.fields, opts.exclude, opts.widgets, formfield_callback) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) else: fields = declared_fields new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(cls, name, bases, attrs): formfield_callback = attrs.pop('formfield_callback', None) try: parents = [b for b in bases if issubclass(b, SQLAModelForm)] except NameError: parents = None declared_fields = forms.forms.get_declared_fields(bases, attrs, False) new_class = super(SQLAModelFormMetaclass, cls) \ .__new__(cls, name, bases, attrs) if not parents: return new_class if 'media' not in attrs: new_class.media = forms.widgets.media_property(new_class) opts = new_class._meta = SQLAModelFormOptions(getattr(new_class, 'Meta', None)) if opts.model: # If a model is defined, extract form fields from it. fields = fields_for_model(opts.model, opts.fields, opts.exclude, opts.widgets, formfield_callback) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) else: fields = declared_fields new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class
def __new__(cls, name, bases, attrs): formfield_callback = attrs.pop('formfield_callback', None) try: parents = [b for b in bases if issubclass(b, ModelForm)] except NameError: # We are defining ModelForm itself. parents = None declared_fields = get_declared_fields(bases, attrs, False) new_class = super(ModelFormMetaclass, cls) \ .__new__(cls, name, bases, attrs) if not parents: return new_class if 'media' not in attrs: new_class.media = media_property(new_class) opts = new_class._meta = ModelFormOptions( getattr(new_class, 'Meta', None)) if opts.model: # If a model is defined, extract form fields from it. if opts.fields is None and opts.exclude is None: # This should be some kind of assertion error once deprecation # cycle is complete. warnings.warn( "Creating a ModelForm without either the 'fields' attribute " "or the 'exclude' attribute is deprecated - form %s " "needs updating" % name, DeprecationWarning, stacklevel=2) ''' if opts.fields == ALL_FIELDS: # sentinel for fields_for_model to indicate "get the list of # fields from the model" opts.fields = None ''' fields = fields_for_model(opts.model, opts.fields, opts.exclude, opts.widgets, formfield_callback, opts.labels, opts.help_texts, opts.error_messages) # make sure opts.fields doesn't specify an invalid field none_model_fields = [k for k, v in fields.items() if not v] missing_fields = set(none_model_fields) - \ set(declared_fields.keys()) if missing_fields: message = 'Unknown field(s) (%s) specified for %s' message = message % (', '.join(missing_fields), opts.model.__name__) raise FieldError(message) # Override default model fields with any custom declared ones # (plus, include all the other declared fields). fields.update(declared_fields) else: fields = declared_fields new_class.declared_fields = declared_fields new_class.base_fields = fields return new_class