def from_model(cls, model): """ Feed me a model, get a ModelState representing it out. """ # Deconstruct the fields fields = [] for field in model._meta.local_fields: name, path, args, kwargs = field.deconstruct() field_class = import_by_path(path) try: fields.append((name, field_class(*args, **kwargs))) except TypeError as e: raise TypeError("Couldn't reconstruct field %s on %s.%s: %s" % ( name, model._meta.app_label, model._meta.object_name, e, )) for field in model._meta.local_many_to_many: name, path, args, kwargs = field.deconstruct() field_class = import_by_path(path) try: fields.append((name, field_class(*args, **kwargs))) except TypeError as e: raise TypeError("Couldn't reconstruct m2m field %s on %s: %s" % ( name, model._meta.object_name, e, )) # Extract the options options = {} for name in DEFAULT_NAMES: # Ignore some special options if name in ["apps", "app_label"]: continue elif name in model._meta.original_attrs: if name == "unique_together": ut = model._meta.original_attrs["unique_together"] options[name] = set(normalize_unique_together(ut)) else: options[name] = model._meta.original_attrs[name] # Make our record bases = tuple( ("%s.%s" % (base._meta.app_label, base._meta.model_name) if hasattr(base, "_meta") else base) for base in model.__bases__ if (not hasattr(base, "_meta") or not base._meta.abstract)) if not bases: bases = (models.Model, ) return cls( model._meta.app_label, model._meta.object_name, fields, options, bases, )
def from_model(cls, model): """ Feed me a model, get a ModelState representing it out. """ # Deconstruct the fields fields = [] for field in model._meta.local_fields: name, path, args, kwargs = field.deconstruct() field_class = import_by_path(path) try: fields.append((name, field_class(*args, **kwargs))) except TypeError as e: raise TypeError("Couldn't reconstruct field %s on %s.%s: %s" % ( name, model._meta.app_label, model._meta.object_name, e, )) for field in model._meta.local_many_to_many: name, path, args, kwargs = field.deconstruct() field_class = import_by_path(path) try: fields.append((name, field_class(*args, **kwargs))) except TypeError as e: raise TypeError("Couldn't reconstruct m2m field %s on %s: %s" % ( name, model._meta.object_name, e, )) # Extract the options options = {} for name in DEFAULT_NAMES: # Ignore some special options if name in ["app_cache", "app_label"]: continue elif name in model._meta.original_attrs: if name == "unique_together": ut = model._meta.original_attrs["unique_together"] options[name] = set(normalize_unique_together(ut)) else: options[name] = model._meta.original_attrs[name] # Make our record bases = tuple( ("%s.%s" % (base._meta.app_label, base._meta.object_name.lower()) if hasattr(base, "_meta") else base) for base in model.__bases__ if (not hasattr(base, "_meta") or not base._meta.abstract) ) if not bases: bases = (models.Model, ) return cls( model._meta.app_label, model._meta.object_name, fields, options, bases, )
def __init__(self, name, unique_together): self.name = name unique_together = normalize_unique_together(unique_together) self.unique_together = set(tuple(cons) for cons in unique_together)