def get_changes(self): # Get the model defs so we can use them for the yield later model_defs = freeze_apps([self.migrations.app_label()]) # Make the model changes for model_name in self.added_models: model = models.get_model(self.migrations.app_label(), model_name) real_fields, meta, m2m_fields = self.split_model_def(model, model_defs[model_key(model)]) yield ("AddModel", { "model": model, "model_def": real_fields, }) # And the field changes for field_desc in self.added_fields: try: model_name, field_name = field_desc.split(".") except (TypeError, ValueError): raise ValueError("%r is not a valid field description." % field_desc) model = models.get_model(self.migrations.app_label(), model_name) real_fields, meta, m2m_fields = self.split_model_def(model, model_defs[model_key(model)]) yield ("AddField", { "model": model, "field": model._meta.get_field_by_name(field_name)[0], "field_def": real_fields[field_name], }) # And the indexes for field_desc in self.added_indexes: try: model_name, field_name = field_desc.split(".") except (TypeError, ValueError): print("%r is not a valid field description." % field_desc) model = models.get_model(self.migrations.app_label(), model_name) yield ("AddIndex", { "model": model, "fields": [model._meta.get_field_by_name(field_name)[0]], })
def forwards_code(self): return self.FORWARDS_TEMPLATE % { "model_name": self.model._meta.object_name, "field_name": self.field.name, "table_name": self.field.m2m_db_table(), "left_field": self.field.m2m_column_name()[:-3], # Remove the _id part "left_model_key": model_key(self.model), "right_field": self.field.m2m_reverse_name()[:-3], # Remove the _id part "right_model_key": model_key(self.field.rel.to), }
def forwards_code(self): return self.FORWARDS_TEMPLATE % { "model_name": self.model._meta.object_name, "field_name": self.field.name, "table_name": self.field.m2m_db_table(), "left_field": self.field.m2m_column_name()[:-3], # Remove the _id part "left_column": self.field.m2m_column_name(), "left_model_key": model_key(self.model), "right_field": self.field.m2m_reverse_name()[:-3], # Remove the _id part "right_column": self.field.m2m_reverse_name(), "right_model_key": model_key(self.field.rel.to), }
def forwards_code(self): if isinstance(self.field, SortedManyToManyField) and self.field.sorted: return self.SORTED_FORWARDS_TEMPLATE % { "model_name": self.model._meta.object_name, "field_name": self.field.name, "table_name": self.field.m2m_db_table(), "left_field": self.field.m2m_column_name()[:-3], # Remove the _id part "left_column": self.field.m2m_column_name(), "left_model_key": model_key(self.model), "right_field": self.field.m2m_reverse_name()[:-3], # Remove the _id part "right_column": self.field.m2m_reverse_name(), "right_model_key": model_key(self.field.rel.to), "sort_field": self.field.sort_value_field_name, } else: return super(AddM2M, self).forwards_code()
def forwards_code(self): if isinstance(self.field, SortedManyToManyField) and self.field.sorted: return self.SORTED_FORWARDS_TEMPLATE % { "model_name": self.model._meta.object_name, "field_name": self.field.name, "table_name": self.field.m2m_db_table(), "left_field": self.field.m2m_column_name()[:-3], # Remove the _id part "left_column": self.field.m2m_column_name(), "left_model_key": model_key(self.model), "right_field": self.field.m2m_reverse_name()[:-3], # Remove the _id part "right_column": self.field.m2m_reverse_name(), "right_model_key": model_key(self.field.rel.to), "sort_field": SORT_VALUE_FIELD_NAME, } else: return super(AddM2M, self).forwards_code()
def get_changes(self): # Get the frozen models for this app model_defs = freeze_apps([self.migrations.app_label()]) for model in models.get_models( models.get_app(self.migrations.app_label())): # Don't do anything for unmanaged, abstract or proxy models if model._meta.abstract or getattr( model._meta, "proxy", False) or not getattr(model._meta, "managed", True): continue real_fields, meta, m2m_fields = self.split_model_def( model, model_defs[model_key(model)]) # Firstly, add the main table and fields yield ("AddModel", { "model": model, "model_def": real_fields, }) # Then, add any indexing/uniqueness that's around if meta: for attr, operation in (("unique_together", "AddUnique"), ("index_together", "AddIndex")): together = eval(meta.get(attr, "[]")) if together: # If it's only a single tuple, make it into the longer one if isinstance(together[0], string_types): together = [together] # For each combination, make an action for it for fields in together: yield (operation, { "model": model, "fields": [ model._meta.get_field_by_name(x)[0] for x in fields ], }) # Finally, see if there's some M2M action for name, triple in m2m_fields.items(): field = model._meta.get_field_by_name(name)[0] # But only if it's not through=foo (#120) if field.rel.through: try: # Django 1.1 and below through_model = field.rel.through_model except AttributeError: # Django 1.2 through_model = field.rel.through if (not field.rel.through) or getattr(through_model._meta, "auto_created", False): yield ("AddM2M", { "model": model, "field": field, })
def get_changes(self): # Get the frozen models for this app model_defs = freeze_apps([self.migrations.app_label()]) for model in models.get_models(models.get_app(self.migrations.app_label())): # Don't do anything for unmanaged, abstract or proxy models if model._meta.abstract or getattr( model._meta, "proxy", False) or not getattr(model._meta, "managed", True): continue real_fields, meta, m2m_fields = self.split_model_def( model, model_defs[model_key(model)]) # Firstly, add the main table and fields yield ("AddModel", { "model": model, "model_def": real_fields, }) # Then, add any indexing/uniqueness that's around if meta: for attr, operation in (("unique_together", "AddUnique"), ("index_together", "AddIndex")): together = eval(meta.get(attr, "[]")) if together: # If it's only a single tuple, make it into the longer one if isinstance(together[0], string_types): together = [together] # For each combination, make an action for it for fields in together: yield (operation, { "model": model, "fields": [model._meta.get_field_by_name(x)[0] for x in fields], }) # Finally, see if there's some M2M action for name, triple in m2m_fields.items(): field = model._meta.get_field_by_name(name)[0] # But only if it's not through=foo (#120) if field.rel.through: try: # Django 1.1 and below through_model = field.rel.through_model except AttributeError: # Django 1.2 through_model = field.rel.through if (not field.rel.through) or getattr(through_model._meta, "auto_created", False): yield ("AddM2M", { "model": model, "field": field, })
def get_changes(self): # Get the model defs so we can use them for the yield later model_defs = freeze_apps([self.migrations.app_label()]) # Make the model changes for model_name in self.added_models: model = models.get_model(self.migrations.app_label(), model_name) real_fields, meta, m2m_fields = self.split_model_def(model, model_defs[model_key(model)]) yield ("AddModel", { "model": model, "model_def": real_fields, }) # And the field changes for field_name in self.added_fields: raise NotImplementedError # And the indexes for index_name in self.added_indexes: raise NotImplementedError
def get_changes(self): # Get the frozen models for this app model_defs = freeze_apps([self.migrations.app_label()]) for model in models.get_models(models.get_app(self.migrations.app_label())): # Don't do anything for unmanaged, abstract or proxy models if ( model._meta.abstract or getattr(model._meta, "proxy", False) or not getattr(model._meta, "managed", True) ): continue real_fields, meta, m2m_fields = self.split_model_def(model, model_defs[model_key(model)]) # Firstly, add the main table and fields yield ("AddModel", {"model": model, "model_def": real_fields}) # Then, add any uniqueness that's around if meta: unique_together = eval(meta.get("unique_together", "[]")) if unique_together: # If it's only a single tuple, make it into the longer one if isinstance(unique_together[0], basestring): unique_together = [unique_together] # For each combination, make an action for it for fields in unique_together: yield ( "AddUnique", {"model": model, "fields": [model._meta.get_field_by_name(x)[0] for x in fields]}, ) # Finally, see if there's some M2M action for name, triple in m2m_fields.items(): field = model._meta.get_field_by_name(name)[0] # But only if it's not through=foo (#120) if (not field.rel.through) or getattr(field.rel.through._meta, "auto_created", False): yield ("AddM2M", {"model": model, "field": field})