zoukankan      html  css  js  c++  java
  • Django3.x和2.x JSONFiled使用

    AttributeError: module 'MySQLdb.constants.FIELD_TYPE' has no attribute 'JSON' while migrating in Django

     __init__.py

    # import pymysql
    # pymysql.version_info = (1, 3, 13, "final", 0)
    # pymysql.install_as_MySQLdb()

    解决升级mysqlclient:

    pip install mysqlclient==2.0.1


    可支持jsonfield的django3和mysqlclient版本,此版本不在使用pymysql,
    直接使用mysqlclient,并且注释掉settings.py 中__init__.py pymysql引用,源码无需修改:

    Django==3.1.2
    mysqlclient==2.0.1

     
    amqp==1.4.9
    anyjson==0.3.3
    appdirs==1.4.4
    asgiref==3.2.10
    billiard==3.3.0.23
    celery==3.1.26.post2
    certifi==2020.6.20
    chardet==3.0.4
    click==7.1.2
    click-didyoumean==0.0.3
    click-repl==0.1.6
    configparser==5.0.1
    coreapi==2.3.3
    coreschema==0.0.4
    distlib==0.3.1
    Django==3.1.2
    django-celery==3.3.1
    django-filter==2.4.0
    django-mysql==3.9.0
    djangorestframework==3.12.1
    drf-extensions==0.6.0
    fdfs-client-py==1.2.6
    filelock==3.0.12
    filetype==1.0.7
    flower==0.9.5
    humanize==3.0.1
    idna==2.10
    importlib-metadata==2.0.0
    importlib-resources==3.0.0
    install==1.3.4
    itypes==1.2.0
    Jinja2==2.11.2
    jsonfield==3.1.0
    jsonpath==0.82
    kombu==3.0.37
    MarkupSafe==1.1.1
    minio==6.0.0
    mutagen==1.45.1
    mysqlclient==2.0.1
    Pillow==8.0.1
    prometheus-client==0.8.0
    prompt-toolkit==3.0.8
    psycopg2==2.8.6
    PyMySQL==0.10.1
    python-dateutil==2.8.1
    pytz==2020.1
    redis==2.10.6
    requests==2.24.0
    six==1.15.0
    sqlparse==0.4.1
    tornado==6.0.4
    uritemplate==3.0.1
    urllib3==1.25.10
    vine==5.0.0
    virtualenv==20.0.35
    wcwidth==0.2.5
    zipp==3.3.1
    

     模型举例:

    def json_default():

    return {}
    class JsonModels(models.Model):
    names=models.CharField(max_length=50)
    attrs =models.JSONField(default=json_default)


    class Meta:
    db_table='tbl_test_jsonfiled'
    verbose_name="jsonfield测试"

    传参:

     空值测试:

    mysql> select * from tbl_test_jsonfiled;
    +----+-----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | id | names | attrs |
    +----+-----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | 1 | testjson | {"id": 2067, "age": 28, "sex": "男", "addr": "河南省济源市北海大道32号", "gold": 100, "name": "小黑", "grade": "天蝎座", "phone": "12345678915"} |
    | 2 | 测试空 | {} |
    +----+-----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------+

    其次还可以设置sql null :
    class JsonModels(models.Model):
    names=models.CharField(max_length=50)
    attrs =models.JSONField(null=True)


    class Meta:
    db_table='tbl_test_jsonfiled'
    verbose_name="jsonfield测试"
     

    mysql> select * from tbl_test_jsonfiled;
    +----+-----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | id | names | attrs |
    +----+-----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | 1 | testjson | {"id": 2067, "age": 28, "sex": "男", "addr": "河南省济源市北海大道32号", "gold": 100, "name": "小黑", "grade": "天蝎座", "phone": "12345678915"} |
    | 2 | 测试空 | {} |
    | 3 | yan | NULL



    django 2.x支持JSONField问题:
    pip install mysqlclient==2.0.1
    pip install django-mysql==3.9.0
    pip install Django==2.2.17
    注意事项:
    1.注册app django_mysql到settings应用里面
    2.使用方法:
    from django.db import models
    from django_mysql.models import JSONField
    
    class JsonModels(models.Model):
        names=models.CharField(max_length=50)
        attrs =JSONField(null=True)
    
    
        class Meta:
            db_table='tbl_jang2_jsonfiled'
            verbose_name="jsonfield测试"
    

    空值测试不传则存为{}

    mysql> select * from tbl_jang2_jsonfiled;
    +----+-----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | id | names | attrs |
    +----+-----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | 1 | jang2test | {} |
    | 2 | jang2test | {"url": "http://127.0.0.1:7888/no/params?id=1001&name=WangXinRan&pwd=testapi", "json": null, "expect": null, "method": "get", "params": null, "headers": {"Content-Type": "application/x-www-form-urlencoded"}, "response_validation": null} |
    +----+-----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


    此外JSONField还可以存入List,string
    curl --location --request POST 'http://127.0.0.1:8000/testjson/' 
    --header 'Content-Type: application/json' 
    --data-raw '
    {
    "names":"listtest",
    "attrs":[1,2,3,"hello",{"hello":"testt"}]
    }'
     

    mysql> select * from tbl_jang2_jsonfiled;
    +----+-----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | id | names | attrs |
    +----+-----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | 1 | jang2test | {} |
    | 2 | jang2test | {"url": "http://127.0.0.1:7888/no/params?id=1001&name=WangXinRan&pwd=testapi", "json": null, "expect": null, "method": "get", "params": null, "headers": {"Content-Type": "application/x-www-form-urlencoded"}, "response_validation": null} |
    | 3 | jang2test | {} |
    | 4 | listtest | [1, 2, 3, "hello", {"hello": "testt"}] |
    +----+-----------+---------------------------------------------------

     
     
  • 相关阅读:
    第13组_16通信3班_045_OSPFv3作业
    RIPng配置(第十三组)
    基于IPV6的数据包分析(更新拓扑加入了linux主机和抓取133icmp包)(第十三组)
    vmware vsphere powercli 因为在此系统中禁止执行脚本
    vmware virtual machine must be running in order to be migrated
    flashback transaction闪回事务查询
    oracle 闪回功能详解
    linux下修改/dev/shm tmpfs文件系统大小
    vmware虚拟机guest系统重启后获得169.254.X.X的ip解决方法
    一键部署 PPTP server
  • 原文地址:https://www.cnblogs.com/SunshineKimi/p/13998870.html
Copyright © 2011-2022 走看看