foreignkey是一种关联字段,将两张表进行关联的方式,我们在dodels.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进行关联,默认取到uid=1的行) user_type_choices=( (1,'superuser'), (2,'commonuser'), (3,'com-commonuser'), ) user_type_id=models.IntegerField(choices=user_type_choices,default=1)
运行下面两条命令:
C:\Users\Liujiangbu.GLOBALE.001\PycharmProjects\untitled3>python manage.py makemigrations
C:\Users\Liujiangbu.GLOBALE.001\PycharmProjects\untitled3>python manage.py migrate
编辑app项目的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>注册成功</p>")
模板中创建userinfo.html,并加入下面内容:
<!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" >是爷们儿就点下 </a> <br /> <a class="menu" href="/group" rel="external nofollow" >是爷们儿就洅点下 </a> </div> <div style="color:#666644"> <h3>添加主机</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>主机列表</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:
执行项目:
在空白处写入内容,会自动添加到数据库中:
表中的DBA就是通过外键关联获取到了
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“django foreignkey(外键)的实现”评论...
更新日志
2024年10月07日
2024年10月07日
- 群星《前途海量 电影原声专辑》[FLAC/分轨][227.78MB]
- 张信哲.1992-知道新曲与精丫巨石】【WAV+CUE】
- 王翠玲.1995-ANGEL【新艺宝】【WAV+CUE】
- 景冈山.1996-我的眼里只有你【大地唱片】【WAV+CUE】
- 群星《八戒 电影原声带》[320K/MP3][188.97MB]
- 群星《我的阿勒泰 影视原声带》[320K/MP3][139.47MB]
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[320K/MP3][148.91MB]
- 刘雅丽.2001-丽花皇后·EMI精选王【EMI百代】【FLAC分轨】
- 齐秦.1994-黄金十年1981-1990CHINA.TOUR.LIVE精丫上华】【WAV+CUE】
- 群星.2008-本色·百代音乐人创作专辑【EMI百代】【WAV+CUE】
- 群星.2001-同步过冬AVCD【环球】【WAV+CUE】
- 群星.2020-同步过冬2020冀待晴空【环球】【WAV+CUE】
- 沈雁.1986-四季(2012梦田复刻版)【白云唱片】【WAV+CUE】
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[FLAC/分轨][257.88MB]
- 《国语老歌 怀旧篇 3CD》[WAV/分轨][1.6GB]