django中将model转换为dict的方法

django中将model转换为dict的方法

from django.forms.models import model_to_dict
from user.model import userprofile

model_to_dict(userprofile.model.get(id=100))

d3 = {‘username‘: ‘zz_yy‘, ‘password‘: ‘zyjzyj‘}
a = UserProfile()
a.username = ‘zz__e‘
a.password = ‘33ff‘
d3 = model_to_dict(a)
print(d3)

#{‘id‘: None, ‘password‘: ‘33ff‘, ‘last_login‘: None, ‘is_superuser‘: #False, ‘username‘: ‘zz__e‘, ‘first_name‘: ‘‘, ‘last_name‘: ‘‘, #‘email‘: ‘‘, ‘is_staff‘: False, ‘is_active‘: True, ‘date_joined‘: #datetime.datetime(2018, 10, 19, 20, 27, 0, 22740), #‘account_person‘: None, ‘if_show_gift_account‘: False, #‘advertisement_type‘: None, ‘account_type‘: ‘advertiser‘, #‘nick_name‘: None, ‘gender‘: ‘male‘, ‘address‘: None, ‘mobile‘: #None, ‘groups‘: <QuerySet []>, ‘user_permissions‘: <QuerySet #[]>}

原文地址:https://www.cnblogs.com/zhaoyingjie/p/9818614.html

时间: 2024-08-06 09:38:18

django中将model转换为dict的方法的相关文章

C语言中将数字转换为字符串的方法(转自c语言中文网)

C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串.以下是用itoa()函数将整数转换为字符串的一个例子: # include <stdio. h># include <stdlib. h> void main (void) { int num = 100; char str[25]; itoa(num, str, 10); printf("The number 'num' is %d and the string 'str' is %

C语言中将数字转换为字符串的方法

C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串.以下是用itoa()函数将整数转换为字符串的一个例子: # include <stdio. h># include <stdlib. h>void main (void);void main (void){    int num = 100;    char str[25];    itoa(num, str, 10);    printf("The number 'num' is %

django 将model转换为字典

from django.forms.models import model_to_dict from projects.models import ProjectInformation site = ProjectInformation.objects.get(id=6) dict = model_to_dict(site) dict {'CRFmethod': '', 'EDCprovider': '', 'acceptancenum': '', 'add_time': datetime.da

C语言中将字符串转换为数字的方法

C语言提供了几个标准库函数,可以将字符串转换为任意类型(整型.长整型.浮点型等)的数字.以下是用atoi()函数将字符串转换为整数的一个例子: # include <stdio. h># include <stdlib. h>void main (void) ;void main (void){    int num;    char * str = "100";    num = atoi(str);    printf("The string 's

将java中数组转换为ArrayList的方法实例(包括ArrayList转数组)

方法一:使用Arrays.asList()方法 1 2 String[] asset = {"equity", "stocks", "gold", "foreign exchange","fixed income", "futures", "options"}; List<String> assetList = Arrays.asList(asset);

Django model distinct 的使用方法

原文: 今天突然有人问起在 django 的 model 里面怎么用 distinct, 对于这种东西,我一向的观点是查看django 的在线文档.于是不加思索的根据在线文档给出了答案,但结果很让人沮丧,运行程序时会报错: NotImplementedError: DISTINCT ON fields is not supported by this database backend, 从字面上看,貌似是因为才用了mysql 的原因,其实不是. django 在线文档讲解 distinct 的连

Python之路【第二十二篇】:Django之Model操作

Django之Model操作 一.字段 AutoField(Field) - int自增列,必须填入参数 primary_key=True BigAutoField(AutoField) - bigint自增列,必须填入参数 primary_key=True 注:当model中如果没有自增列,则自动会创建一个列名为id的列 from django.db import models class UserInfo(models.Model): # 自动创建一个列名为id的且为自增的整数列 usern

【Django】--Model字段

所有字段 AutoField(Field) --int自增列,必须填入参数primary_key=True BigAutoField(AutoField) --bigint自增列,必须填入参数primary_key=True 注意:当model中没有自增列,则自动会创建一个列名为id的列 from django.db import models class UserInfo(models.Model): #自动创建一个列名为id的且为自增的整数列 username = models.CharFi

Django之Model操作

Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去调用数据访问层执行数据库操作 import MySQLdb def GetList(sql): db = MySQLdb.connect(user='root', db='wupeiqidb', passwd='1234', host='localhost') cursor = db.cursor() cursor.