1 项目路径结构树

2 models创建类

from django.db import models

class UserType(models.Model):

    '''

    用户类型

    '''

    title = models.CharField(max_length=12)

class UserInfo(models.Model):

    '''

    用户表

    '''

    name = models.CharField(max_length=16)

    age = models.IntegerField()

 ut = models.ForeignKey('UserType')

  

CharField :char类型

IntegerField: int类型

ForeignKey: 关联表

3 检测app是否注册项目

添加app01

4 初始化数据库

初始化db.sqlite3数据库

1.创建表结构

>>python manage.py makemigrations

Migrations for 'app01':

  app01\migrations\0001_initial.py

    - Create model UserInfo

    - Create model UserType

- Add field ut to userinfo

  

2. 初始化数据库

>> python manage.py migrate

Operations to perform:

  Apply all migrations: admin, app01, auth, contenttypes, sessions

Running migrations:

  Applying contenttypes.0001_initial... OK

  Applying auth.0001_initial... OK

  Applying admin.0001_initial... OK

  Applying admin.0002_logentry_remove_auto_add... OK

  Applying app01.0001_initial... OK

  Applying contenttypes.0002_remove_content_type_name... OK

  Applying auth.0002_alter_permission_name_max_length... OK

  Applying auth.0003_alter_user_email_max_length... OK

  Applying auth.0004_alter_user_username_opts... OK

  Applying auth.0005_alter_user_last_login_null... OK

  Applying auth.0006_require_contenttypes_0002... OK

  Applying auth.0007_alter_validators_add_error_messages... OK

  Applying auth.0008_alter_user_username_max_length... OK

  Applying sessions.0001_initial... OK

  

5 创建表数据

1)     urls映射关系

urlpatterns = [

    url(r'^create_data/', views.create_data),

]

  

2)     创建视图函数

from django.shortcuts import render,redirect,HttpResponse

from app01 import models

# Create your views here.

def create_data(request):

    '''创建书籍'''

    #models.UserType.objects.create(title='普通用户')

    #models.UserType.objects.create(title='白金用户')

    #models.UserType.objects.create(title='砖石用户')

'''创建用户'''

  #models.UserInfo.objects.create(name='alex',age=32,ut_id=1)

   #models.UserInfo.objects.create(name='egon',age=42,ut_id=2)

   #models.UserInfo.objects.create(name='yuan',age=32,ut_id=3)

   #models.UserInfo.objects.create(name='naza',age=32,ut_id=1)

'''获取数据'''

    result = models.UserInfo.objects.all()

    for obj in result:

    print(obj.name,obj.age)

return HttpResponse('...')

  

'''

   alex 32

   egon 42

   yuan 32

   naza 32

'''

  

跨表操作获取数据

result = models.UserInfo.objects.all()

    for obj in result:

        print(obj.name,obj.age,obj.ut.title)

alex 32 普通用户

egon 42 白金用户

yuan 32 砖石用户

naza 32 普通用户

  

6 访问初始化数据

http://127.0.0.1:8000/create_data/

  

Django视图之ORM连表操作一的更多相关文章

  1. Django框架06 /orm多表操作

    Django框架06 /orm多表操作 目录 Django框架06 /orm多表操作 1. admin相关操作 2. 创建模型 3. 增加 4. 删除 5. 修改 6. 基于对象的跨表查询 7. 基于 ...

  2. Django框架05 /orm单表操作

    Django框架05 /orm单表操作 目录 Django框架05 /orm单表操作 1. orm使用流程 2. orm字段 3. orm参数 4. orm单表简单增/删/改 5. orm单表查询 5 ...

  3. Django之模型---ORM 多表操作

    多表操作 创建表模型 from django.db import models # Create your models here. class Author(models.Model): nid = ...

  4. django框架基础-ORM单表操作-长期维护

    ###############    单表操作-添加数据    ################ import os if __name__ == '__main__': os.environ.set ...

  5. Django之模型---ORM 单表操作

    以上一随笔中创建的book表为例讲解单表操作 添加表记录 方式一 # create方法的返回值book_obj就是插入book表中的python葵花宝典这本书籍纪录对象 book_obj=Book.o ...

  6. python django基础五 ORM多表操作

    首先在创建表的时候看下分析一下 1.作者表和作者详细地址表  一对一关系 理论上谁都能当主表 把Author设置成主表 au=models.OneToOneField(to='AuthorDetail ...

  7. Django 学习 之ORM多表操作

    一.创建模型 1.模型关系整理 创建一对一的关系:OneToOne("要绑定关系的表名") 创建一对多的关系:ForeignKey("要绑定关系的表名") 创建 ...

  8. django框架基础-ORM跨表操作-长期维护

    ###############    一对一跨表查询    ################ import os if __name__ == '__main__': os.environ.setde ...

  9. Django视图之ORM数据库查询操作API

    查询表记录 查询相关API 操作:models.表名.objects.方法() all(): 查询所有结果 filter(**kwargs): 它包含了与所给筛选条件相匹配的对象 get(**kwar ...

随机推荐

  1. BZOJ [Ctsc2002] Award 颁奖典礼 解题报告

    [Ctsc2002] Award 颁奖典礼 Description IOI2002的颁奖典礼将在YONG-IN Hall隆重举行.人们在经历了充满梦幻的世界杯之后变得更加富于情趣.为了使颁奖典礼更具魅 ...

  2. Cydia Substrate based DexDumper's weakness

    得益于Cydia Substrate框架,HOOK Native函数变得简单,也给脱壳带来方便. 像ijiami免费版,360,classes.dex被加密到so文件并运行时释放到内存,因此针对相关函 ...

  3. 跳跃表 https://61mon.com/index.php/archives/222/

    跳跃表(英文名:Skip List),于 1990 年 William Pugh 发明,是一个可以在有序元素中实现快速查询的数据结构,其插入,查找,删除操作的平均效率都为 . 跳跃表的整体性能可以和二 ...

  4. HDU2444 :The Accomodation of Students(二分图染色+二分图匹配)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  5. HDU5957 Query on a graph(拓扑找环,BFS序,线段树更新,分类讨论)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5957 题意:D(u,v)是节点u和节点v之间的距离,S(u,v)是一系列满足D(u,x)<=k的点 ...

  6. 002.比较vector对象是否相等

    1.使用vector模板 //编写一段程序,比较vector对象是否相等 //注:该例类似于一个[彩票游戏] #include <iostream> #include <ctime& ...

  7. 使用html5的Geolocation API实现定位

    公司有个需求,需要获取用户的位置,所以看了下html5的Geolocation 这个新东西,发现挺好用的. <!DOCTYPE html> <html> <body> ...

  8. ZooKeeper入门(四)

    入门:使用ZooKeeper的协调分布式应用 这个文档使你对ZooKeeper快速入门,它主要针对想尝试它的开发者.并且包含简单的单机的ZooKeeper服务的安装说明,一些验证是否运行的命令,和一个 ...

  9. luaj luaoc 回调函数传递的一些小总结

    问题场景:我们的游戏在支付时,由于第三方支付比较费时,可能在支付的过程中,我们lua写的cocos2dx项目会断网,我们的游戏有自动重连的机制.我就想,如果断线好了以后,支付完成了,那在断网之前传入的 ...

  10. c语言目录操作总结

    =================================================== char *getcwd( char *buffer, int maxlen ); (获取当前目 ...