Django learning---Day3-Django model view query advanced---2

Django learning day 3 (02)


content

  • Class Student Table Association Concept
  • url routing
  • Defining Student Expansion Table Classes
  • Define the add student method
  • Add student front-end rendering page
  • Write a method to get the students corresponding to the expansion table
  • Through student information, get his/her family information

Class Student Table Association Concept

1. One-to-one - 1-1 - OneToOneField
The primary key and foreign key are in a one-to-one relationship. In the association table, only the id of one main table can be associated.
Expand the table to find the
main Extended table information: main table object. model_name of the associated table

2. One-to-many - 1-n - see next introduction
3. Many-to-many - m-n - see next introduction

4.on_delete on_delete=models.PROTECT The main table is deleted, and the slave table cannot be deleted. The
default cascade, on_delete=models.CASCADE is deleted from the main table, and the slave table is also deleted
. set_null The main table is deleted, and the associated field of the slave table is set to empty
protect protected , cannot be deleted

url routing

from django.conf.urls import url
from stu import views
urlpatterns = [
    # 创建学生信息
    url(r'^add_students/', views.add_students),
]

Defining Student Expansion Table Classes

class StudentInfo(models.Model):
    s_addr = models.CharField(max_length=100)
    # 与学生关联的,一对一
    stu = models.OneToOneField(Student)
    class Meta:
        db_table = 'stu_info'

Define the add student method

def add_students(request):
    if request.method == 'GET':
        return render(request, 'add_students.html')
    if request.method == 'POST':
        # 创建学生信息
        s_name = request.POST.get('name')
        s_gender = 1 if request.POST.get('gender') else 0
        s_birth = request.POST.get('birth')
        s_tel = request.POST.get('tel')
        s_Chinese = request.POST.get('Chinese')
        s_math = request.POST.get('math')
        Student.objects.create(
            s_name = s_name,
            s_gender = s_gender,
            s_birth = s_birth,
            s_tel = s_tel,
            s_Chinese = s_Chinese,
            s_math = s_math
        )
        return render(request, 'add_students.html')

Add student front-end rendering page

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加学生信息</title>
</head>
<body>
    <form action="/stu/add_students/" method="post">
        <table>
            <tr>
                <td>姓名</td>
                <td>性别</td>
                <td>生日</td>
                <td>电话</td>
                <td>语文</td>
                <td>数学</td>
            </tr>
            <tr>
                <td><input type="text" name="name"></td>
                <td><input type="text" name="gender"></td>
                <td><input type="date" name="birth"></td>
                <td><input type="text" name="tel"></td>
                <td><input type="text" name="Chinese"></td>
                <td><input type="text" name="math"></td>
            </tr>
        </table>
        <input type="submit" value="提交">
    </form>
</body>
</html>

Write a method to get the students corresponding to the expansion table

Find student information by expanding the student's address

url(r'^search_stu/', views.search_stu),

1. Writing method one, but generally not written in this way, you need to search the database twice, and you will be beaten by the boss

def search_stu(request):
    # 通过拓展表学生的地址,来查找学生信息
    # 查找addr=北京的学生信息
    # 写法一, 但一般不这么写,需要查找2次数据库,会被老板打
    # stus = StudentInfo.objects.filter(s_addr='北京')
    # stu = stus[0]
    # search_stu = Student.objects.filter(id=stu.id)
    # return render(request, 'search_stu.html', {'search_stu': search_stu})
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    {% for i in search_stu %}
        姓名:{{ i.s_name }}
    {% endfor %}
</body>
</html>

2. Writing method 2

stus = StudentInfo.objects.filter(s_addr__contains='北京')
    stu = stus[0]
    search_stu = stu.stu
    return render(request, 'search_stu.html', {'search_stu': search_stu})

This query does not require iteration, all remove the for loop in the html page

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    姓名:{{ search_stu.s_name }}
</body>
</html>

Through student information, get his/her family information

stu = Student.objects.filter(s_name='白百合').first()
    search_stu = StudentInfo.objects.filter(id=stu.id)
    # search_stu = StudentInfo.objects.filter(stu_id=stu.id)
    return render(request, 'search_stu.html', {'search_stu': search_stu})
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    {% for i in search_stu %}
        地址:{{ i.s_addr }}
    {% endfor %}
</body>
</html>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325779675&siteId=291194637