MCPcopy
hub / github.com/django/django / __new__

Method __new__

django/forms/models.py:275–344  ·  view source on GitHub ↗
(mcs, name, bases, attrs)

Source from the content-addressed store, hash-verified

273
274class ModelFormMetaclass(DeclarativeFieldsMetaclass):
275 def __new__(mcs, name, bases, attrs):
276 new_class = super().__new__(mcs, name, bases, attrs)
277
278 if bases == (BaseModelForm,):
279 return new_class
280
281 opts = new_class._meta = ModelFormOptions(getattr(new_class, "Meta", None))
282
283 # We check if a string was passed to `fields` or `exclude`,
284 # which is likely to be a mistake where the user typed ('foo') instead
285 # of ('foo',)
286 for opt in ["fields", "exclude", "localized_fields"]:
287 value = getattr(opts, opt)
288 if isinstance(value, str) and value != ALL_FIELDS:
289 msg = (
290 "%(model)s.Meta.%(opt)s cannot be a string. "
291 "Did you mean to type: ('%(value)s',)?"
292 % {
293 "model": new_class.__name__,
294 "opt": opt,
295 "value": value,
296 }
297 )
298 raise TypeError(msg)
299
300 if opts.model:
301 # If a model is defined, extract form fields from it.
302 if opts.fields is None and opts.exclude is None:
303 raise ImproperlyConfigured(
304 "Creating a ModelForm without either the 'fields' attribute "
305 "or the 'exclude' attribute is prohibited; form %s "
306 "needs updating." % name
307 )
308
309 if opts.fields == ALL_FIELDS:
310 # Sentinel for fields_for_model to indicate "get the list of
311 # fields from the model"
312 opts.fields = None
313
314 fields = fields_for_model(
315 opts.model,
316 opts.fields,
317 opts.exclude,
318 opts.widgets,
319 opts.formfield_callback,
320 opts.localized_fields,
321 opts.labels,
322 opts.help_texts,
323 opts.error_messages,
324 opts.field_classes,
325 # limit_choices_to will be applied during ModelForm.__init__().
326 apply_limit_choices_to=False,
327 form_declared_fields=new_class.declared_fields,
328 )
329
330 # make sure opts.fields doesn't specify an invalid field
331 none_model_fields = {k for k, v in fields.items() if not v}
332 missing_fields = none_model_fields.difference(new_class.declared_fields)

Callers

nothing calls this directly

Calls 8

FieldErrorClass · 0.90
ModelFormOptionsClass · 0.85
fields_for_modelFunction · 0.85
itemsMethod · 0.45
differenceMethod · 0.45
joinMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected