Django项目初始化

初始化操作

创建app

python manage.py startapp appname

MySQL连接

SETTING.py

1
2
3
4
5
6
7
8
9
10
11
12
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'xxx',
'HOST':'localhost',
'PORT':'3306',
'USER':'username',
'PASSWORD':'password'
}
}

LANGUAGE_CODE = 'zh-hans'

init.py

1
2
import pymysql
pymysql.install_as_MySQLdb()

数据库模型(models.py)

1
2
3
4
5
6
7
8
from Django.db import models

class xxx(models.Model):
a = models.AutoField(primary_key=True)
……

class Meta:
db_table='tableName'

数据库迁移

1
2
python manage.py makemigrations
python manage.py migrate

mongoDB连接

djongo

安装

  1. 必须完全按照以下版本安装引用来源
    1
    2
    3
    4
    5
    6
    7
    8
    asgiref==3.5.0
    Django==4.0.3
    djongo==1.3.6
    dnspython==2.2.1
    pymongo==3.12.1
    python-snappy==0.6.1
    pytz==2022.1
    sqlparse==0.2.4
  2. 在您的项目settings.py文件中,添加:
    1
    2
    3
    4
    5
    6
    DATABASES = {
    'default': {
    'ENGINE': 'djongo',
    'NAME': 'your-db-name',
    }
    }

    setting.py配置数据库

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    DATABASES = {
    'default': {
    'ENGINE': 'djongo',
    'NAME': 'mdb',
    'ENFORCE_SCHEMA': False,
    'CLIENT': {
    'host': 'ip',
    'port': 27017,
    'username': 'xx',
    'password': 'xx',
    'authSource': 'mdb',
    'authMechanism': 'SCRAM-SHA-1'
    },
    }
    }

定义模型

Blog是一个抽象类,相当于二级集合,在下面Entry集合中的blog字段里面用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from djongo import models

class Blog(models.Model):
name = models.CharField(max_length=100)

class Meta:
abstract = True
//定义抽象类后不会在数据库中建表

class Entry(models.Model):
blog = models.EmbeddedField(
model_container=Blog
)
headline = models.CharField(max_length=255)

在view的自定义函数中这样用。通过字典的形式给blog集合内的值赋值

1
2
3
4
5
6
e = Entry()
e.blog = {
'name': 'Djongo'
}
e.headline = 'The Django MongoDB connector'
e.save()

报错处理

1. Array and Embedded Fields vs Error Abstract models cannot be instantiated

在读取数据库时报错,抽象类不能被实例化。说明blog这个字段出现了问题,无法读取数据库

解决办法

  • 根据文档的说法在Model模型中添加一个objects字段,示例如下
    1
    2
    3
    4
    5
    6
    class Entry(models.Model):
    blog = models.EmbeddedField(
    model_container=Blog,
    )
    headline = models.CharField(max_length=255)
    objects = models.DjongoManager()
  • 添加objects字段后就就可以使用pymongo的命令来读取数据库了
    1
    2
    3
    4
    5
    6
    7
    8
    9
    index = [i for i in Entry.objects.mongo_aggregate([
    {
    '$match': {
    'headline': 'xx'
    }
    },
    ])]

    return index

2.Object of type ‘ObjectId’ is not JSON serializable

读取数据库后通过JsonResponse返回报错

mongodb读取后返回的是一个orderdict(有序字典)它是有元素的先后顺序的。通过Json.load也无法将其格式化。

解决办法

1
2
3
4
from bson import json_util

data = json_util.dumps(data)
return HttpResponse(data)

路由配置

app/urls.py(需自建)

1
2
3
4
5
6
7
8
9
import App.views
from django.contrib import admin
from django.urls import path

urlpatterns = [
path('',App.views.function),
path……
]

urls.py

1
2
3
4
5
6
7
8
from django.contrib import admin
from django.urls import path, include
from App import urls

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('App.urls'))
]

跨域问题解决

https://www.cnblogs.com/majingjie/p/11152052.html

django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

ERRORS:
?: (corsheaders.E014) Origin ‘http://123.56.127.98/' in CORS_ORIGIN_WHITELIST should not have path
?: (corsheaders.E014) Origin ‘http://onestep.wanqqq29.cn/' in CORS_ORIGIN_WHITELIST should not have path


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!