南强小屋 Design By 杰米

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,并加入下面内容:

django foreignkey(外键)的实现

<!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:

django foreignkey(外键)的实现

执行项目:

django foreignkey(外键)的实现

在空白处写入内容,会自动添加到数据库中:

django foreignkey(外键)的实现

表中的DBA就是通过外键关联获取到了

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
django,foreignkey,django,外键

南强小屋 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
南强小屋 Design By 杰米

评论“django foreignkey(外键)的实现”

暂无django foreignkey(外键)的实现的评论...

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。