I want to remove the class declaration of the model but want to keep the records and the table on the database. How can I do that?
Now doing “python manage.py migrate” will migrate the old models data and its relations to the new one. Then do “makemigrations” and “migrate”! That's it. You have successfully removed a Django model and its relations without introducing any errors in your project.
To create an object, instantiate it using keyword arguments to the model class, then call save() to save it to the database. This performs an INSERT SQL statement behind the scenes. Django doesn't hit the database until you explicitly call save() . The save() method has no return value.
Once you have defined your models, you need to tell Django you're going to use those models. Do this by editing your settings file and changing the INSTALLED_APPS setting to add the name of the module that contains your models.py .
From the documentation: If False, no database table creation or deletion operations will be performed for this model. This is useful if the model represents an existing table or a database view that has been created by some other means.
Stop Django managing your model by setting the Meta
class attribute managed
to False
(default is True
) like in the following:
class SomeModel(models.Model):
....
class Meta:
managed = False
Then run python manage.py makemigrations
, which should create a migration telling you something like
- Change Meta options on something
Run that migration by python manage.py migrate
, which will stop Django from managing that model and then delete it from your code base. The migration will look like:
class Migration(migrations.Migration):
dependencies = [
('blah', '0001_initial'),
]
operations = [
migrations.AlterModelOptions(
name='something',
options={'managed': False},
),
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With