数据库设置及多对多笔记

数据库设置字符编码
CREATE DATABASE `test2` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

修改表的编码
alter table app01_host charset='utf8';

授权
grant all privileges on s14_day20.* to 'lwb'@'%' identified by '123456';

乱码
show variables like "%char%";
SET character_set_xxx='utf8';


securecrt
 终端=》外观,在右边的窗口和文本外观设置项中,有一项字符编码,选择UTF-8,这样就能解决乱码问题。


Ajax
    $.ajax({
    url:'/host',   --->提交到哪
    type:"Post",   --->以什么方式提交
    data:{'k1':123,'k2':'root'},  --->提交什么数据
    success:function(data){         --->确认对方执行成功后自动执行这个函数
        //data是服务器端返回的字符串
        var obj=JSON.parse(data); 字符串转字典对象
    }
    })


    建议:永远让服务端返回一个字典

    return HttpResponse(json.dumps(字典))

    $.get(url="xx",data={},success=)
    $.getJson
    $.post


创建多对多:
    方式一:自定义关系表
    class Host(models.Model):
        nid=models.AutoField(primary_key=True)
        hostname=models.CharField(max_length=32,db_index=True)
        ip=models.GenericIPAddressField(protocol='ipv4',db_index=True)
        port=models.IntegerField()
        b=models.ForeignKey('Business',to_field='id',on_delete = models.CASCADE)

    class Application(models.Model):
        name=models.CharField(max_length=32)

    class HostToApp(models.Model):
        hobj=models.ForeignKey(to='Host',to_field='nid',on_delete = models.CASCADE)
        aobj=models.ForeignKey(to='Application',to_field='id',on_delete = models.CASCADE)
        status=models.CharField(max_length=32)

    models.Host.object.create(hobj_id='',aobj_id='')

    方式二:自动创建关系表
    class Host(models.Model):
        nid=models.AutoField(primary_key=True)
        hostname=models.CharField(max_length=32,db_index=True)
        ip=models.GenericIPAddressField(protocol='ipv4',db_index=True)
        port=models.IntegerField()
        b=models.ForeignKey('Business',to_field='id',on_delete = models.CASCADE)

    class Application(models.Model):
        name=models.CharField(max_length=32)
        r=models.ManyToManyField('Host')

    无法直接对第三张表操作
    间接操作第三张表
    obj=Application.objects.get('id=1')
    obj.name
    obj.r.add(1)  Application id=1 host id=1
    obj.r.add(2)  Application id=1 host id=2
    obj.r.add(3,4,5)
    obj.r.add(*[1,2,3,4])  1 1, 1 2, 1 3, 1,4

    obj.r.remove(1)
    obj.r.remove(2,3)
    obj.r.remove(*[4,5,6])

    obj.r.clear()  清除Application id=1的所有关系

    obj.r.set([3,6,7])  1 3, 1 6, 1 7

    obj.r.all()  所有相关的主机对象 QuerySet Host对象

原文地址:https://www.cnblogs.com/leiwenbin627/p/11028829.html