Python-web之Django项目连接已存在的mysql数据库

在django开发过程中,需要从自己的mysql数据库中读取数据并展示,由于表格是存在的,不需要像其他django项目一样在数据库中创建专门的表格,大致过程如下

1. 设置setting中的database信息

# setting.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': '<你的mysql数据库名>',
        'USER': '',
        'PASSWORD': '',
        'HOST': '127.0.0.1',
        'PORT': 3306
    }
}

2.反向生成models.py

利用django的inspectdb,django通过setting当中的database配置信息找到数据库表,并输出到默认输出设备

python .\manage.py inspectdb

输出大致如下,不同项目以及表格输出会有所区别,但是形式基本一样

# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
#   * Rearrange models' order
#   * Make sure each model has one field with primary_key=True
#   * Make sure each ForeignKey has `on_delete` set to the desired behavior.
#   * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table
# Feel free to rename the models, but don't rename db_table values or field names.
from __future__ import unicode_literals

from django.db import models

class Riderecords(models.Model):
    rider = models.CharField(db_column='Rider', max_length=32)  # Field name made lowercase.
    trainnum = models.CharField(db_column='TrainNum', max_length=128)  # Field name made lowercase.
    seatnum = models.CharField(db_column='SeatNum', max_length=16)  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'Riderecords'

class AuthGroup(models.Model):
    name = models.CharField(unique=True, max_length=80)

    class Meta:
        managed = False
        db_table = 'auth_group'

class AuthUser(models.Model):
    password = models.CharField(max_length=128)
    last_login = models.DateTimeField(blank=True, null=True)
    is_superuser = models.IntegerField()
    username = models.CharField(unique=True, max_length=150)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    email = models.CharField(max_length=254)
    is_staff = models.IntegerField()
    is_active = models.IntegerField()
    date_joined = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'auth_user'


class AuthUserGroups(models.Model):
    user = models.ForeignKey(AuthUser, models.DO_NOTHING)
    group = models.ForeignKey(AuthGroup, models.DO_NOTHING)

    class Meta:
        managed = False
        db_table = 'auth_user_groups'
        unique_together = (('user', 'group'),)


class DjangoSession(models.Model):
    session_key = models.CharField(primary_key=True, max_length=40)
    session_data = models.TextField()
    expire_date = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'django_session'

直接拷贝到models.py文件中去

3.将改动应用到数据库表

执行两个命令

python manage.py makemigrations
python manage.py migrate

 

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

到这步基本大多数人都可以解决自己的问题了,但是我的情况又比较复杂,可以看到我上面的models里的表格语言中,我的表格是没有主键之类的字段的,而django会默认给你的表格一个id字段,用于标记每条记录,网上说的内容大致是去掉那一行就行了,然而我的输出内容中,并没有被自动添加id字段,没得删,然而在编写views过程中,使用

models.Personinfo.objects.all()

获得的内容传递给html前端之后,读取内容的时候总是会报错,找不到id这个字段,一脸懵啊,表格中也没有这个字段,models中也没有添加这个字段,但是通过objects.all()方式获得的内容,总是会出现这个问题,头皮发麻

经过多方查阅,发现了objects的这个方法

models.Personinfo.objects.values_list('name', 'sex', 'age')

利用values_list()方法传递所有字段名来获取,勉强算是解决了这个问题

你可能感兴趣的:(python-web)