zoukankan      html  css  js  c++  java
  • 如何用Django建立一个后台CRM系统02

    往数据库中添加数据 在Teminal对数据库进行操作

      -objects.all查找

      -objects.first  获取表中对象(即一行)

      -xxxx .order_set.all 反向查询 一般是从父程序查询子程序 即知道一个对象的具体属性xxxx 通过外键关联  反找出另一个表的数据

    像最后的父子模型

    #***(1)Returns all customers from customer table
    customers = Customer.objects.all()
    
    #(2)Returns first customer in table
    firstCustomer = Customer.objects.first()
    
    #(3)Returns last customer in table
    lastCustomer = Customer.objects.last()
    
    #(4)Returns single customer by name
    customerByName = Customer.objects.get(name='Peter Piper')
    
    #***(5)Returns single customer by name
    customerById = Customer.objects.get(id=4)
    
    #***(6)Returns all orders related to customer (firstCustomer variable set above)
    firstCustomer.order_set.all()
    
    #(7)***Returns orders customer name: (Query parent model values)
    order = Order.objects.first() 
    parentName = order.customer.name
    
    #(8)***Returns products from products table with value of "Out Door" in category attribute
    products = Product.objects.filter(category="Out Door")
    
    #(9)***Order/Sort Objects by id
    leastToGreatest = Product.objects.all().order_by('id') 
    greatestToLeast = Product.objects.all().order_by('-id') 
    
    
    #(10) Returns all products with tag of "Sports": (Query Many to Many Fields)
    productsFiltered = Product.objects.filter(tags__name="Sports")
    
    '''
    (11)Bonus
    Q: If the customer has more than 1 ball, how would you reflect it in the database?
    A: Because there are many different products and this value changes constantly you would most 
    likly not want to store the value in the database but rather just make this a function we can run
    each time we load the customers profile
    '''
    
    #Returns the total count for number of time a "Ball" was ordered by the first customer
    ballOrders = firstCustomer.order_set.filter(product__name="Ball").count()
    
    #Returns total count for each product orderd
    allOrders = {}
    
    for order in firstCustomer.order_set.all():
        if order.product.name in allOrders:
            allOrders[order.product.name] += 1
        else:
            allOrders[order.product.name] = 1
    
    #Returns: allOrders: {'Ball': 2, 'BBQ Grill': 1}
    
    
    #RELATED SET EXAMPLE
    class ParentModel(models.Model):
        name = models.CharField(max_length=200, null=True)
    
    class ChildModel(models.Model):
        parent = models.ForeignKey(Customer)
        name = models.CharField(max_length=200, null=True)
    
    parent = ParentModel.objects.first()
    #Returns all child models related to parent
    parent.childmodel_set.all()

    对于动态路由(正则):

      -在path  <>里面加入primary key  <str:pk>

     path('customer/<str:pk>/', views.customer,name='customer'),

      -向函数里面传参数

    def customer(request, pk):

    对模板进行动态数据的输入和url路由

        -{{ xxx }} 是模板里动态对数据的输入填充   

          其中值得注意的是在函数中对数据库取值时利用反向传参求得数据(父子模型)             -在多项选择中过滤选项

    order = customer.order_set.all()
    <td>{{order.customer.name}}</td>

    Dlivered = order.objects.filter(status='Dlivered') //status是html页面 dlivered是其中一个多项选择

        -{% for i in date %} 可用于循环结构

             href ="{%  url 'customer' customer.id  %}" id是用get传参数给customer的primary key

    {% for customer in customers %}
                        <tr>
                            <td><a class="btn btn-sm btn-info" href="{% url 'customer' customer.id %}">View</a></td>
                            <td>{{customer.name}}</td>
                            <td>{{customer.phone}}</td>
                        </tr>
      {% endfor %}       

        -{%  url  'xxx'  % } 用于url的跳转  <a  href = "{ % url  'xxx' %}"></a>   (其中xxx为url的name)

          需要在url.py中在path后面加上name

     path('',views.home,name='home'),
        path('products/',views.products,name='products'),
        path('customer/<str:pk>/', views.customer,name='customer'),

          在html中的a标签里面的href属性设置跳转

    <td> 
      <a class="btn btn-sm btn-info" href="{% url'customer'customer.id%}">
    Views
      </a>
    </td>
  • 相关阅读:
    Ne10编译安装
    AR中的SLAM(一)
    2016总结&2017计划
    DSO之光度标定
    DBoW2库介绍
    Kinect2.0 for Mac开箱
    SVO原理解析
    【漏洞复现】PHPCMS wap模块 SQL注入(附EXP)
    【编程开发】PHP---面向对象教程
    【编程开发】Python---列表
  • 原文地址:https://www.cnblogs.com/kangkang1999/p/13378765.html
Copyright © 2011-2022 走看看