• Django学习路16_获取学生所在的班级名


    在 urls.py 中先导入getgrades
    
    from django.conf.urls import url
    
    from app5 import views
    
    urlpatterns = [
        url(r'getuser/',views.get_user),
        url(r'getusers/',views.get_users),
        url(r'getorder',views.getorders),
        url(r'getgrades',views.getgrades),
    ]

    在 views.py 中编写 getgrades 函数
    
    def getgrades(request):
        grades = Grade.objects.filter(student__s_name='qq')
        # 班级里包含名字为 qq 的
        for grade in grades:
            print(grade.g_name)
    
        return HttpResponse("获取成功")

    此前的准备工作:创建数据库表 
    
    class Grade(models.Model):
        g_name = models.CharField(max_length=16)
    
    class Student(models.Model):
        s_name = models.CharField(max_length= 16)
        s_grade = models.ForeignKey(Grade,on_delete=True)
        # 设置班级外键

    2020-05-11

  • 相关阅读:
    HelpersRainCaptcha
    HelpersPHPMailer
    HelpersPassword
    HelpersPagination
    HelpersNumber
    HelpersHooks
    HelpersGeoCode
    HelpersFastCache
    HelpersDocument
    eclipse 设置jsp页面为HTML5
  • 原文地址:https://www.cnblogs.com/hany-postq473111315/p/12868775.html
Copyright © 2020-2023  润新知