zoukankan      html  css  js  c++  java
  • django一对多模型以及如何在前端实现

    models.py

    class xm(models.Model):
        xmID=models.AutoField(primary_key=True)
        xmTitle=models.CharField('项目标题',max_length=50)
        xmDate=models.DateField('时间',auto_now_add=True)
      
    
        class Meta:
            db_table="std_web_xm"
            verbose_name="项目概况"
            verbose_name_plural=verbose_name
    
        def __unicode__(self):
            return self.title
        def __str__(self):
            #强制转换为int类型
            return str(self.xmID)#为了使在xadmin中成功显示为xmID
    
    class xmProgress(models.Model):
        xmProgress_ID=models.AutoField(primary_key=True)
        xmProgress = models.CharField('项目进展', max_length=50, blank=True)
        xmDesc = models.CharField('详情', max_length=50, blank=True)
        #关联主键xmID ,级联删除
        cid=models.ForeignKey("xm",on_delete=models.CASCADE)
        xmProgress_Date=models.DateField('更新时间',auto_now_add=True)
    class Meta:
            db_table="std_web_xmProgress"
            verbose_name="项目详情"
            verbose_name_plural=verbose_name
    
        def __unicode__(self):
            return self.title

    注册到adminx.py中

    class XmAdmin(object):
        list_display = ['xmTitle','xmDate']
        #style_fields = {"xmContent": "ueditor"}
    
    class XmProgressAdmin(object):
        list_display=['cid','xmProgress','xmProgress_Date','xmDesc']
    
    xadmin.site.register(xm,XmAdmin)
    xadmin.site.register(xmProgress,XmProgressAdmin)

    views.py

    def xm_detail(request,xmID):
        xm_detail=xm.objects.get(pk=xmID)
        xmProgress_detail=xmProgress.objects.filter(cid=xm_detail)
        return render(request,'xmgk/xmgk_detail.html',{'xmProgress_detail':xmProgress_detail,'xm_detail':xm_detail})

    前端代码

     

    
    
    {% for line in xmProgress_detail %}
    <tr>
      <td>{{ line.xmProgress_Date|date:"Y-m-d" }}</td>
      <td>{{ line.xmProgress }}</td>
      <td>{{ line.xmDesc }}</td>
    </tr>

    {% endfor %}
  • 相关阅读:
    Nhibernate代码生成器v2.1中文版
    在asp.net中生成16位随机密码
    IIS 启动不了(发生意外错误0x8ffe2740)
    NET代码生成器
    Linux系统
    VS2005快捷键大全
    ASP+ACCESS数据库中文乱码问题解决
    如何配置ASP.NETOracle 9i 远程登陆数据库
    ASP.NET获取汉字拼音的首字母
    checkbox 实时操作,勾选后变色[带演示]
  • 原文地址:https://www.cnblogs.com/1998lu/p/10022927.html
Copyright © 2011-2022 走看看