最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Django中數(shù)據(jù)庫模型類models.py的示例分析-創(chuàng)新互聯(lián)

這篇文章主要介紹Django中數(shù)據(jù)庫模型類models.py的示例分析,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)主營沙縣網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,App定制開發(fā),沙縣h5成都微信小程序搭建,沙縣網(wǎng)站營銷推廣歡迎沙縣等地區(qū)企業(yè)咨詢

如下所示:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
# Create your models here.
# 一對一關(guān)系:數(shù)據(jù)庫中兩個表中數(shù)據(jù)的對應(yīng)關(guān)系
# 一個賬戶對應(yīng)著一個聯(lián)系人,而一個聯(lián)系人有一個賬戶
# 一對一關(guān)系是通過在兩個表之間定義相同的主鍵來完成
class Account(models.Model):
 username = models.CharField(max_length=20, null=True, blank=True, verbose_name=u'用戶名')
 password = models.CharField(max_length=40, null=True, blank=True, verbose_name=u'密碼')
 register_date = models.DateField(auto_now_add=True, null=True, blank=True, verbose_name=u'注冊時間')
 class Meta:
  db_table = 'Account'
 # 該函數(shù)是負(fù)責(zé)展示該類對象的詳細(xì)信息的函數(shù),根據(jù)需要自定義展示的內(nèi)容
 def __unicode__(self):
  return 'Account:%s'%self.username
class Contact(models.Model):
 # 在Contact中,關(guān)聯(lián)Account表,讓兩個表的數(shù)據(jù)產(chǎn)生聯(lián)系
 # 第一個參數(shù):是被關(guān)聯(lián)的模型名稱
 # 第二個參數(shù):當(dāng)Account中的一條數(shù)據(jù)被刪除的時候,與之對應(yīng)的Contact數(shù)據(jù)也會被刪除
 account = models.OneToOneField(Account, on_delete=models.CASCADE, primary_key=True)
 address = models.CharField(max_length=100, null=True)
 code = models.CharField(max_length=20, null=True)
 mobile = models.CharField(max_length=20, null=True)
 class Meta:
  db_table = 'Contact'
 def __unicode__(self):
  # self.account:通過聯(lián)系人對象反向查詢該信息所屬的人
  return 'Contact:%s-%s-%s'%(self.account.username,self.address,self.mobile)
# ORM:關(guān)系映射對象,把傳統(tǒng)的SQL語句封裝成了類和對象的形式,在操作表中的數(shù)據(jù)記錄時,就像在操作類和對象
# 一對一的正向查詢和反向查詢
a1 = Account(username='dawei',password='333')
a1.save()
c1 = Contact(account=a1,address='xinmi',code='450000',mobile='13212344321')
c1.save()
print a1.contact# 正向查詢,通過賬戶查詢該賬戶對應(yīng)的詳細(xì)信息
print c1.account# 反向查詢,通過詳細(xì)信息查詢信息對應(yīng)的賬戶
# a1.contact.mobile
# a1.contact.address
# a1.contact.code
# c1.account.username
# c1.account.password
# 刪除賬戶,對應(yīng)的聯(lián)系人信息也會被刪除
# a1.delete()

以上是“Django中數(shù)據(jù)庫模型類models.py的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!


分享標(biāo)題:Django中數(shù)據(jù)庫模型類models.py的示例分析-創(chuàng)新互聯(lián)
URL鏈接:http://fisionsoft.com.cn/article/phhpi.html