MCPcopy
hub / github.com/django/django / from_model

Method from_model

django/db/migrations/state.py:799–932  ·  view source on GitHub ↗

Given a model, return a ModelState representing it.

(cls, model, exclude_rels=False)

Source from the content-addressed store, hash-verified

797
798 @classmethod
799 def from_model(cls, model, exclude_rels=False):
800 """Given a model, return a ModelState representing it."""
801 # Deconstruct the fields
802 fields = []
803 for field in model._meta.local_fields:
804 if getattr(field, "remote_field", None) and exclude_rels:
805 continue
806 if isinstance(field, models.OrderWrt):
807 continue
808 name = field.name
809 try:
810 fields.append((name, field.clone()))
811 except TypeError as e:
812 raise TypeError(
813 "Couldn't reconstruct field %s on %s: %s"
814 % (
815 name,
816 model._meta.label,
817 e,
818 )
819 )
820 if not exclude_rels:
821 for field in model._meta.local_many_to_many:
822 name = field.name
823 try:
824 fields.append((name, field.clone()))
825 except TypeError as e:
826 raise TypeError(
827 "Couldn't reconstruct m2m field %s on %s: %s"
828 % (
829 name,
830 model._meta.object_name,
831 e,
832 )
833 )
834 # Extract the options
835 options = {}
836 for name in DEFAULT_NAMES:
837 # Ignore some special options
838 if name in ["apps", "app_label"]:
839 continue
840 elif name in model._meta.original_attrs:
841 if name == "unique_together":
842 ut = model._meta.original_attrs["unique_together"]
843 options[name] = set(normalize_together(ut))
844 elif name == "indexes":
845 indexes = [idx.clone() for idx in model._meta.indexes]
846 for index in indexes:
847 if not index.name:
848 index.set_name_with_model(model)
849 options["indexes"] = indexes
850 elif name == "constraints":
851 options["constraints"] = [
852 con.clone() for con in model._meta.constraints
853 ]
854 else:
855 options[name] = model._meta.original_attrs[name]
856 # If we're ignoring relationships, remove all field-listing model

Calls 9

normalize_togetherFunction · 0.90
set_name_with_modelMethod · 0.80
_set_creation_counterMethod · 0.80
appendMethod · 0.45
cloneMethod · 0.45
getMethod · 0.45
indexMethod · 0.45
copyMethod · 0.45
addMethod · 0.45