Django 1.11.1 + Correct steps for database table operation

Posted by railanc4309 on Wed, 12 Jun 2019 21:55:36 +0200


Wallpaper. jpg

Hello everyone! Yes, I have issued a "pseudo" tutorial. Originally this article was originally called "Django database table operation summary: based on Django 1.11.1", but I changed it because Django has done all that we should do. We don't need to think about dealing with the database itself at all. When I updated the website (yes, that ugly topic), I considered deleting some fields and deleting them. Some field attributes were modified, so I inquired a lot of tutorials, their tutorials should be too old, leading me to step by step operation, the final result is all wrong!

It's a pit-by-pit lesson. Until I discussed this problem with my friend "The sun is my enthalpy" recently, I did a new operation and prepared to reproduce the mistake I made to him at that time. As a result, I found that as long as I modified one of the models.py in Django project and synchronized one time, there would be no problem and perfect solution to our needs. So, we need to It's just a correct procedure.

Consider your needs first. I need to delete the same field of all data on a server that has been deployed and running for a long time, modify the properties of a field in a table, and finally add a ManyToManyField field.

After looking at the needs, let's see how to implement them concretely.

My code snippet (intercept snippet):

# Table of articles
class Post(models.Model):
    title = models.CharField(max_length=70, verbose_name=u'Title')
    excerpt = models.CharField(max_length=100, verbose_name=u'abstract')
    imgurl = models.CharField(max_length=200, verbose_name=u'Cover Chart')
    body = models.TextField(verbose_name=u'content')
    category = models.ForeignKey(Category, verbose_name=u'Classification')
    plate = models.ForeignKey(Plate, verbose_name=u'Subordinate plate')
    love_num = models.IntegerField(default=0, verbose_name=u'Number of Favorites')
    browse_num = models.IntegerField(default=0, verbose_name=u'Number of visits')
    created_time = models.DateTimeField(verbose_name=u'Creation time')
    modified_time = models.DateTimeField(verbose_name=u'Final modification time')

Now I want to delete the field "platform". According to the online tutorial, we need to delete the field from the database and delete the table, but it doesn't need to be so troublesome. We just need to remove the following line of code.

plate = models.ForeignKey(Plate, verbose_name=u'Subordinate plate')

Then enter the management.py directory in the terminal and enter the synchronization command:

python manage.py makemigrations
python manage.py migrate

To do this, I also need to modify the properties of a field in a table:

class Links(models.Model):
    title = models.CharField(max_length=30, verbose_name=u'Website Name')
    url = models.CharField(max_length=20, verbose_name=u'Website Home Page')
    created_time = models.DateTimeField(verbose_name=u'Creation time')

    class Meta:
        verbose_name = 'Friendship Links'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title

Because I did not consider the fact that there are many fields in the homepage of some websites when I first designed the table, which led to the character size limitation when I added friendship links later. So I need to modify this field. The traditional way is:

Firstly modify the field properties in the models.py file instead of synchronizing, then go to Mysql's command line and modify the field properties directly:

alter table table name mod column name attribute;

For example, before me:

url = models.CharField(max_length=20, verbose_name=u'Website Home Page')

I want to change it to

 url = models.CharField(max_length=70, verbose_name=u'Website Home Page')

The order is:

alter table monitor_links modify turl varchar(70);

Actually, it doesn't need to be so troublesome at all. It just needs to modify the field attributes directly and synchronize them.

Last requirement: add a new ManyToManyField field.

The approach is: first add a Class, which is no different from the previous operation of creating tables, and then synchronize the database so that it can set up tables:

# Many-to-many
class Tags(models.Model):
    name = models.CharField(max_length=100)

Add this field to the list of articles:

# Table of articles
class Post(models.Model):
    title = models.CharField(max_length=70, verbose_name=u'Title')
    excerpt = models.CharField(max_length=100, verbose_name=u'abstract')
    imgurl = models.CharField(max_length=200, verbose_name=u'Cover Chart')
    body = models.TextField(verbose_name=u'content')
    category = models.ForeignKey(Category, verbose_name=u'Classification')
    # plate = models.ForeignKey(Plate, verbose_name=u's plate')
    love_num = models.IntegerField(default=0, verbose_name=u'Number of Favorites')
    browse_num = models.IntegerField(default=0, verbose_name=u'Number of visits')
    created_time = models.DateTimeField(verbose_name=u'Creation time')
    modified_time = models.DateTimeField(verbose_name=u'Final modification time')
    # New fields added
    tags = models.ManyToManyField(Tags, null=True, blank=True, default='')

Final synchronization! These database operations should be all you need. See you next time.

Topics: Database Django Python MySQL