django User model operation

this tutorial will guide us to know how to manipulate django User model.

Read User object derived from database

from django.contrib.auth.models import User

# Those two lines are different even if there is only one user 'admin' who registered before

auser = User.objects.get(username = 'admin')   # unique only one var
buser = User.objects.filter(username = 'admin') # var type the same as " allusers " allusers = User.objects.all()

Now let's check out how it shows.

in python shell,

>>>allusers

[<User: admin>]

>>>auser

<User: admin>

>>>buser

[<user: admin>]

>>>allusers == buser

Flase

Now let's see how to write data for User

Creating users

The most direct way to create users is to use the included create_user() helper function:

>>> from django.contrib.auth.models import User
>>> user = User.objects.create_user('john', 'lennon@thebeatles.com', 'johnpassword') # At this point, user is a User object that has already been saved
# to the database. You can continue to change its attributes
# if you want to change other fields.
>>> user.last_name = 'Lennon'
>>> user.save()

If you have the Django admin installed, you can also create users interactively.

 For more information about user authentication, user permissions, user groups, Please visit
 

authenticate (验证身份)

the operation downside will directly talk to database model

from django.contrib import auth
user = auth.authenticate(username='john', password='secret')
if user is not None:
print "Correct!"
else:
print "Invalid password."

django User model的更多相关文章

  1. Django之Model操作

    Django之Model操作 本节内容 字段 字段参数 元信息 多表关系及参数 ORM操作 1. 字段 字段列表 AutoField(Field) - int自增列,必须填入参数 primary_ke ...

  2. Django的Model上都有些什么

    Django的Model上都有些什么 modelinfo= ['DoesNotExist', 'MultipleObjectsReturned', '__class__', '__delattr__' ...

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

    Django之Model操作   一.字段 AutoField(Field) - int自增列,必须填入参数 primary_key=True BigAutoField(AutoField) - bi ...

  4. Scrapy中使用Django的Model访问数据库

    Scrapy中使用Django的Model进行数据库访问 当已存在Django项目的时候,直接引入Django的Model来使用比较简单 # 使用以下语句添加Django项目的目录到path impo ...

  5. django使用model创建数据库表使用的字段

    Django通过model层不可以创建数据库,但可以创建数据库表,以下是创建表的字段以及表字段的参数.一.字段1.models.AutoField 自增列= int(11) 如果没有的话,默认会生成一 ...

  6. Django之Model组件

    Model组件在django基础篇就已经提到过了,本章介绍更多高级部分. 一.回顾 1.定义表(类) ##单表 from django.db import models class user(mode ...

  7. django中将model转换为dict的方法

    django中将model转换为dict的方法 from django.forms.models import model_to_dict from user.model import userpro ...

  8. Python学习---django之Model语法180124

    django之Model语法[Models] 1    django默认支持sqlite,mysql, oracle,postgresql数据库. <1> sqlite django默认使 ...

  9. Django的model查询操作 与 查询性能优化

    Django的model查询操作 与 查询性能优化 1 如何 在做ORM查询时 查看SQl的执行情况 (1) 最底层的 django.db.connection 在 django shell 中使用 ...

随机推荐

  1. Java-继承特性

    继承的特点: 1.提高了代码的复用性. 2.让类与类之间发生了关系,有了这个关系,才有了多态的特性. (注意:千万不要为了获取其他类的功能,简化代码而继承:必须是类与类之间有所属关系才可以继承,所属关 ...

  2. SSAS系列——【08】多维数据(程序展现Cube)

    原文:SSAS系列--[08]多维数据(程序展现Cube) 1.引用DLL? 按照之前安装的MS SQLServer的步骤安装完成后,发现在新建的项目中“Add Reference”时居然找不到Mic ...

  3. 基于jsoup的Java服务端http(s)代理程序-代理服务器Demo

    亲爱的开发者朋友们,知道百度网址翻译么?他们为何能够翻译源网页呢,iframe可是不能跨域操作的哦,那么可以用代理实现.直接上代码: 本Demo基于MVC写的,灰常简单,copy过去,简单改改就可以用 ...

  4. JAVA基本的编程50称号(7-9称号)详细解释

    一个.叙述性说明 1.输入一行字符.在这些信件统计.空格.出现频率的数字和其他字符的总数和每一个字符.         程序分析:使用String类的matchs()分别统计符合正則表達式的每类字符的 ...

  5. SQL data reader reading data performance test

    /*Author: Jiangong SUN*/ As I've manipulated a lot of data using SQL data reader in recent project. ...

  6. Python 2.7.3 Time与DateTime格式化

    import time import datetime class TimeX: '''时间工具,目前用于格式化时间''' @staticmethod def GetLocalTimeString_T ...

  7. 使用TeamCity对项目进行可持续集成管理

    使用TeamCity对项目进行可持续集成管理 一.可持续集成管理   持续集成,CI:即Continuous integration. 可持续集成的概念是基于团队(小组)协作开发而提出来的,为了提高团 ...

  8. Spring IOC之Bean 概述

    1.Bean概述 一个Spring IOC容器管理一个或者多个bean.这些bean是根据你提供给容器的配置数据信息创建的,例如XML形式的的定义. 在容器内部,这些bean的定义表示为BeanDef ...

  9. iPhone、iPad、iPadMini界面设计标准

    一个:iPhone 4.0' Display: iPhone 5.iPhone 5S.iPhone 5C. 解析度:1136 * 960 设计标准參照下图iPhone5 3.5' Display:   ...

  10. leetcode第30题--Next Permutation

    problem: Implement next permutation, which rearranges numbers into the lexicographically next greate ...