django foreignkey(外鍵)的實(shí)現(xiàn)
foreignkey是一種關(guān)聯(lián)字段,將兩張表進(jìn)行關(guān)聯(lián)的方式,我們?cè)赿odels.py里寫入要生成的兩張表:
class Usergroup(models.Model): uid=models.AutoField(primary_key=True) caption=models.CharField(max_length=64,null=True) ctime=models.DateField(auto_now_add=True,null=True) uptime=models.DateField(auto_now=True,null=True) class Userinfo(models.Model): username=models.CharField(max_length=32,blank=True) password=models.FileField(max_length=60,help_text='pwd') email=models.CharField(max_length=60) test=models.EmailField(max_length=20,null=True,error_messages={'invalid':'shurumima'}) user_group=models.ForeignKey('Usergroup',to_field='uid',default=1) #這里與上面的Usergroup表的uid進(jìn)行關(guān)聯(lián),默認(rèn)取到uid=1的行) user_type_choices=( (1,'superuser'), (2,'commonuser'), (3,'com-commonuser'), ) user_type_id=models.IntegerField(choices=user_type_choices,default=1)
運(yùn)行下面兩條命令:
C:\Users\Liujiangbu.GLOBALE.001\PycharmProjects\untitled3>python manage.py makemigrations
C:\Users\Liujiangbu.GLOBALE.001\PycharmProjects\untitled3>python manage.py migrate
編輯app項(xiàng)目的views.py加入下面兩段:
def user_info(request): if request.method == "GET": #userlist = test.objects.all() userlist2=models.Userinfo.objects.all() #獲取Userinfo表的所有信息 # return render(request,'userinfo.html',{'userlist':userlist}) return render(request, 'userinfo.html', {'userlist2': userlist2}) #模板引用 elif request.method == "POST": u = request.POST.get('user') pp= request.POST.get('pwd') #test.objects.create(username=u,depno=pp) models.Userinfo.objects.create(username=u,password=pp,user_group_id=1) return HttpResponse("<p>注冊(cè)成功</p>")
模板中創(chuàng)建userinfo.html,并加入下面內(nèi)容:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>666666(runoob.com)</title> </head> <body style="background-color:silver"> <div> <div style="color:#00FF00"> <a class="menu" href="/home-bak" rel="external nofollow" >是爺們兒就點(diǎn)下 </a> <br /> <a class="menu" href="/group" rel="external nofollow" >是爺們兒就洅點(diǎn)下 </a> </div> <div style="color:#666644"> <h3>添加主機(jī)</h3> <form method="POST" action="/userinfo"> <input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}"> <input type="text" name="user"> <input type="text" name="pwd"> <input type="submit" name="添加"> </form> <h3>主機(jī)列表</h3> <ul> {% for i in userlist2 %} <li><a href="/datail" rel="external nofollow" >{{ i.username }}</a> <span>{{ i.user_group.caption }}</span> </li> {% endfor %} </ul> </div> </div> </body> </html>
然后添加url:
執(zhí)行項(xiàng)目:
在空白處寫入內(nèi)容,會(huì)自動(dòng)添加到數(shù)據(jù)庫中:
表中的DBA就是通過外鍵關(guān)聯(lián)獲取到了
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解如何從TensorFlow的mnist數(shù)據(jù)集導(dǎo)出手寫體數(shù)字圖片
這篇文章主要介紹了詳解如何從TensorFlow的mnist數(shù)據(jù)集導(dǎo)出手寫體數(shù)字圖片,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Python實(shí)現(xiàn)監(jiān)控一個(gè)程序的運(yùn)行情況
這篇文章主要為大家介紹了Python如何實(shí)現(xiàn)監(jiān)控一個(gè)程序的運(yùn)行情況,然后視情況將進(jìn)程殺死并重啟,文中的示例代碼簡(jiǎn)潔易懂,需要的可以參考一下2023-05-05Python實(shí)現(xiàn)自動(dòng)識(shí)別并批量轉(zhuǎn)換文本文件編碼
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)自動(dòng)識(shí)別并批量轉(zhuǎn)換文本文件編碼的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-03-03Tensorflow tf.nn.atrous_conv2d如何實(shí)現(xiàn)空洞卷積的
這篇文章主要介紹了Tensorflow tf.nn.atrous_conv2d如何實(shí)現(xiàn)空洞卷積的,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04python實(shí)現(xiàn)超市進(jìn)銷存管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)超市進(jìn)銷存管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06pytorch下大型數(shù)據(jù)集(大型圖片)的導(dǎo)入方式
今天小編就為大家分享一篇pytorch下大型數(shù)據(jù)集(大型圖片)的導(dǎo)入方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01Python 身份驗(yàn)證和授權(quán)庫使用詳解(python jwt庫)
python_jwt是一個(gè)Python庫,用于生成、解析和驗(yàn)證JSON Web Tokens(JWT),它完全符合JWT標(biāo)準(zhǔn)規(guī)范(RFC 7519),并提供了簡(jiǎn)單而強(qiáng)大的API,使得用戶可以輕松地在Python應(yīng)用中實(shí)現(xiàn)JWT功能,通過本文的介紹,深入探討了python_jwt庫的功能特性、使用方法以及應(yīng)用場(chǎng)景2021-01-01python判斷一組數(shù)呈上升還是下降趨勢(shì)的操作方法
要判斷一組數(shù)(數(shù)列)是呈上升趨勢(shì)、下降趨勢(shì)還是無明顯趨勢(shì),我們可以比較數(shù)列中相鄰元素的差值,這篇文章主要介紹了python?如何判斷一組數(shù)呈上升還是下降趨勢(shì),需要的朋友可以參考下2024-06-06Python實(shí)現(xiàn)Linux的find命令實(shí)例分享
本文給大家分享的是使用python簡(jiǎn)單實(shí)現(xiàn)模擬linux的find命令的實(shí)例代碼,推薦給大家,希望大家能夠喜歡2017-06-06