def do_migrations(self): """ Do a custom migration without leaving any migration files around. """ old_desc = DynamicModel.objects.filter(name=self.name).first() try: OldModel = apps.get_model("django_models_from_csv", self.name) except LookupError: OldModel = None # TODO: for some reason force_new=True (removed now, but forces re- # creation of the model class) makes the model show up multiple # times in the admin. but setting this to False makes the meta # schema migrations not work properly w/o restarting the system. # I need to fix this, along with custom meta in general, but for # now we need to reinstate sane working defaults. NewModel = construct_model(self) new_desc = self ModelSchemaEditor(OldModel).update_table(NewModel) for new_field in NewModel._meta.fields: if new_field.name == "id": continue old_field = self.find_old_field(OldModel, new_field) if old_field: FieldSchemaEditor(old_field).update_column(NewModel, new_field)
def do_migrations(self): """ Do a custom migration without leaving any migration files around. """ old_desc = DynamicModel.objects.filter(name=self.name).first() try: OldModel = apps.get_model("django_models_from_csv", self.name) except LookupError: OldModel = None NewModel = construct_model(self) new_desc = self # TODO: if new or name changed, run this (or just run it) ModelSchemaEditor(OldModel).update_table(NewModel) # TODO: figure out a way to reconcile the old columns/table # with the new. we need a way to figure out what's been removed # and added vs what's been changed. # TODO: handle delete by checking for old fields not existing in new for new_field in NewModel._meta.fields: if new_field.name == "id": continue # TODO: ONLY call this when it's changed! old_field = self.find_old_field(OldModel, new_field) FieldSchemaEditor(old_field).update_column(NewModel, new_field)
def __init__(self, *args, **kwargs): """ Initialize the schema editor with the currently registered model and the initial name. """ super().__init__(*args, **kwargs) self._initial_name = self.name initial_model = self.get_model(name=self._initial_name) self.schema_editor = ModelSchemaEditor(initial_model)
def delete(self, **kwargs): # first drop the table, we have to do this first, else # django will complain about no content type existing Model = apps.get_model("django_models_from_csv", self.name) ModelSchemaEditor().drop_table(Model) # then remove django app and content-types/permissions app_config = apps.get_app_config("django_models_from_csv") wipe_models_and_permissions(app_config, self.name) # finally kill the row super().delete(**kwargs) # delete it from the django app registry try: del apps.all_models["django_models_from_csv"][self.name] except KeyError as err: raise LookupError("'{}' not found.".format(self.name)) # Unregister the model from the admin, before we wipe it out try: admin.site.unregister(Model) except admin.sites.NotRegistered: pass
def delete(self, **kwargs): Model = apps.get_model("django_models_from_csv", self.name) ModelSchemaEditor().drop_table(Model) self.unregister_model(self.name) super().delete(**kwargs)