6.6、Django -解决DateTimeField创建的时间字段保存到MySQL的datatime(6)格式问题

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u012111465/article/details/89888914

1、问题

问题,直接在APP的models.py中通过create_time = models.DateTimeField(default = timezone.now)来创建时间,会带来一些问题,比如,发现MySQL中create_time字段为datatime(6)格式。下面就来解决一下。
通过查阅发现,Django的DateTimeField默认为datatime(6)。通过如下查找对应文件: django/db/backends/mysql/base.py

@cached_property 
def data_types(self): 
    if self.features.supports_microsecond_precision: 
        return dict(self._data_types, DateTimeField='datetime(6)', TimeField='time(6)') 
    else: 
        return self._data_types

2、解决

通过在models.py中增加:

from django.db import models
from django_mysql.models import JSONField
import django.utils.timezone as timezone
from django.db.backends.mysql.base import DatabaseFeatures # 关键设置
DatabaseFeatures.supports_microsecond_precision = False # 关键设置

class RubikJson(models.Model):
    # 此处省略多多。。。。。。
    create_time = models.DateTimeField(default = timezone.now)

即可以解决毫秒等精确度保存的问题。
重新migrations之后,查阅数MySQL据库,create_time为datetime格式。

展开阅读全文

php上传字段存到mysql

10-13

[code=PHP] if($dsql->ExecuteNoneQuery($sql))rn rn //文件存储路径rn $file_path="../upload/";rn //664权限为文件属主和属组用户可读和写,其他用户只读rn if(is_dir($file_path)!=TRUE) mkdir($file_path,0664);rn //定义允许上传的文件扩展rn $ext_arr = array("gif", "jpg", "jpeg", "png", "bmp", "txt", "zip", "rar");rn //获得文件扩展名rn $temp_arr = explode(".", $_FILES["photo"]["name"]);rn $file_ext = array_pop($temp_arr);rn $file_ext = trim($file_ext);rn $file_ext = strtolower($file_ext);rn //获取上传文件的信息赋给变量rn $upfile = $_FILES['photo'];rn $photo = $upfile;rn //以时间戳重命名文件rn $new_name = time().".".$file_ext;rn //将文件移动到存储目录下rn move_uploaded_file($_FILES["photo"]["tmp_name"],"$file_path" . $new_name);rn //向数据表写入文件存储信息以便管理rn foreach($Items as $key=>$val)rn rn $val['price'] = str_replace(",","",$val['price']);rn $dsql->ExecuteNoneQuery("INSERT INTO `#@__shops_products` (`aid`,`oid`,`userid`,`title`,`price`,`buynum`)rn VALUES ('$val[id]','$OrdersId','$userid','$val[title]','$val[price]','$val[buynum]');");rn rn $sql = "INSERT INTO `#@__shops_userinfo` (`userid`,`oid`,`consignee`,`address`,`zip`,`tel`,`email`,`qq`,`cphm`,`t1`,`t2`,`period`,`des`,`dabh`,`photo`,`scanning`)rn VALUES ('$userid','$OrdersId','$postname','$address','$zip','$tel','$email','$qq','$cphm','$t1','$t2','$period','$des','$dabh','$photo','$scanning');rn ";rn $dsql->ExecuteNoneQuery($sql);rn rn elsern rn ShowMsg("更新订单时出现错误!".$dsql->GetError(),"-1");rn exit();rn rn else rn $sql = "UPDATE `#@__shops_orders`rn SET `cartcount`='$CartCount',`price`='$priceCount',`ip`='$ip',`stime`='$stime',pid='$pid',paytype='$paytype',dprice='$dprice',priceCount='$lastpriceCount'rn WHERE oid='$OrdersId' AND userid='$userid' ;";rn if($dsql->ExecuteNoneQuery($sql))rn rn $sql = "UPDATE `#@__shops_userinfo`rn SET `consignee`='$postname',`address`='$address',`zip`='$zip',`tel`='$tel',`email`='$email',`qq`='$qq',`cphm`='$cphm',`t1`='$t1',`t2`='$t2',`period`='$period',`des`='$des',`dabh`='$dabh',`photo`='$photo',`scanning`='$scanning'rn WHERE oid='$OrdersId';";rn $dsql->ExecuteNoneQuery($sql);rn rn elsern rn echo $dsql->GetError();rn exit;rn rn unset($sql);rn [/code]rnrn为什么表单提交后photo字段显示成Array而不是图片上传后的路径?文件是能上传提交上去的,那个地方出了问题还请高手赐教!!!! 论坛

没有更多推荐了,返回首页