Model description and problem collection of Django frame model

Posted by janm2009 on Tue, 12 Nov 2019 19:25:02 +0100

1. How to determine whether modal is successful?
  1. Open the database directly, and check whether there are tables you created and whether the table structure is consistent in the database
  2. Open the user model / migrations / directory to view the PY file inside. 0001 ﹣ initial.py is the PY generated during the initial generation, and then a new py (not an override) will be generated when the model changes. You can view the structure in py.
# -*- coding: utf-8 -*-
# Generated by Django 1.11.20 on 2019-05-11 13:36
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='User',
            fields=[
                ('u_name', models.CharField(blank=True, max_length=20)),
                ('u_id', models.CharField(max_length=255, primary_key=True, serialize=False)),
                ('u_psw', models.CharField(max_length=64)),
                ('u_phone', models.CharField(blank=True, max_length=24)),
                ('u_status', models.IntegerField(blank=True)),
            ],
        ),
    ]

2. Is the created database structure inconsistent with the customized model?
  1. Please check whether there is', 'after each field defined by class. If there is a comma, the field will fail
#There is no extra comma between each field, otherwise it will fail
class User(models.Model):
    u_name = models.CharField(max_length=20,blank=True)		 	#User name, can be blank
    u_id = models.CharField(max_length=255,primary_key=True) 	#User id, set as primary key
    u_psw = models.CharField(max_length=64,blank=False) 		#User password, cannot be empty
    u_phone=models.CharField(max_length=24,blank=True) 			#User contact information, can be blank
    u_status=models.IntegerField(blank=True)					#User status, can be empty
3. Can't rebuild automatically after deleting the table manually?

Reference resources: https://blog.csdn.net/androidstarjack/article/details/89216892

  1. This is because there is cache in django, so we need to delete the corresponding database and cache under the model. For example, my database
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

So I need to delete the db.sqlite3 database files in the project directory and those files in the model


At this time, execute the command again.

4.modal operation manual

Reference resources: https://blog.csdn.net/weixin_37773766/article/details/80330221

Topics: Database Django