django foreignkey(外键)的实现
作者:老夫刘某 时间:2023-03-15 17:35:51
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就是通过外键关联获取到了
来源:https://www.jianshu.com/p/b11f24771a69
标签:django,foreignkey,外键
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python读csv文件去掉一列后再写入新的文件实例
2022-05-13 14:04:12
Pytorch 保存模型生成图片方式
2023-04-20 17:42:17
利用Python 实现图片转字符画
2022-05-06 11:31:01
![](https://img.aspxhome.com/file/2023/0/92510_0s.png)
详解Spring Boot中使用Flyway来管理数据库版本
2024-01-14 21:04:12
python sort、sort_index方法代码实例
2022-12-16 00:43:17
Python提取支付宝和微信支付二维码的示例代码
2021-04-08 19:38:42
![](https://img.aspxhome.com/file/2023/2/70242_0s.jpg)
MySQL 行转列详情
2024-01-22 18:19:58
Django后台admin的使用详解
2023-11-21 14:57:50
![](https://img.aspxhome.com/file/2023/2/83942_0s.png)
python 获取谷歌浏览器保存的密码
2022-05-21 21:39:49
PHP 引用的概念
2023-11-14 21:24:28
Python使用pymysql模块操作mysql增删改查实例分析
2024-01-24 03:56:48
python Scrapy爬虫框架的使用
2023-07-16 01:03:09
sqlserver获取各种形式的时间
2024-01-20 17:33:50
Python自动化办公之群发邮件案例详解
2023-09-08 17:10:36
![](https://img.aspxhome.com/file/2023/9/120929_0s.jpg)
Linux系统中为php添加pcntl扩展
2023-09-04 02:58:15
从ThinkPHP3.2.3过渡到ThinkPHP5.0学习笔记图文详解
2023-09-04 10:21:28
![](https://img.aspxhome.com/file/2023/6/55586_0s.png)
Python提示[Errno 32]Broken pipe导致线程crash错误解决方法
2022-12-02 02:20:42
MySQL中LAG()函数和LEAD()函数的使用
2024-01-27 14:05:35
![](https://img.aspxhome.com/file/2023/9/122889_0s.png)
详解Mysql数据库平滑扩容解决高并发和大数据量问题
2024-01-19 12:52:13
![](https://img.aspxhome.com/file/2023/1/107921_0s.png)
在Vue中实现添加全局store
2024-05-03 15:12:38
![](https://img.aspxhome.com/file/2023/6/130186_0s.png)