django foreignkey(外键)的实现

yipeiwu_com6年前Python基础

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就是通过外键关联获取到了

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python开发之for循环操作实例详解

本文实例讲述了python开发之for循环操作。分享给大家供大家参考,具体如下: 下面是我做的一些学习记录供大家参考: #基本的for循环语句 test_list = [2,"Jon...

python中的文件打开与关闭操作命令介绍

1.文件打开与关闭 在python,使用open函数,可以打开一个已经存在的文件,或者创建一个新文件 open(文件名,访问模式)。 f = open('test.txt', 'w...

对Python信号处理模块signal详解

Python中对信号处理的模块主要是使用signal模块,但signal主要是针对Unix系统,所以在Windows平台上Python不能很好的发挥信号处理的功能。 要查看Python中...

Python基于回溯法解决01背包问题实例

Python基于回溯法解决01背包问题实例

本文实例讲述了Python基于回溯法解决01背包问题。分享给大家供大家参考,具体如下: 同样的01背包问题,前面采用动态规划的方法,现在用回溯法解决。回溯法采用深度优先策略搜索问题的解,...

利用python将json数据转换为csv格式的方法

假设.json文件中存储的数据为: {"type": "Point", "link": "http://www.dianping.com/newhotel/22416995", "c...