我正在使用表现来测试我的小Django应用程序.
我已经创建了user_management.feature包含此场景的文件:
Scenario: register
Given I can access registration form
When I put "doctor" in "username" field
And I put "tardisBlue" in "password" field
And I put "doctor@tardis.com" in "email" field
And I press the "Register" button
Then the registration is successful
And I am logged in
Run Code Online (Sandbox Code Playgroud)
Everythig工作正常.
我想要开发的下一个功能是在文件中project_management.feature:
Scenario: create a project
Given I am logged in
When I go to the home page
And I click on "Create new Project" link
And I fill …Run Code Online (Sandbox Code Playgroud) 我们使用 Pydantic 来设置“域模型”并在应用程序层中使用它们。
import datetime
from decimal import Decimal
from pydantic import BaseModel
class Person(BaseModel):
name: str
birth_date: datetime.date
height: Decimal
# other 100 fields, most of them Decimal
Run Code Online (Sandbox Code Playgroud)
现在我们需要将其中一个模型导出到 CSV,第一个实现很简单:
import csv
from typing import Iterable
def store(persons: Iterable[Person]):
fieldnames = list(Person.schema()["properties"].keys())
with open("/tmp/test.csv", "w") as fp:
writer = csv.DictWriter(fp, fieldnames=fieldnames)
writer.writeheader()
for person in persons:
writer.writerow(person.dict())
Run Code Online (Sandbox Code Playgroud)
但还有更多,因为我们希望所有小数都限制为小数点后两位,我们怎样才能实现这一点呢?
注意:不会将序列化逻辑放入域模型中,但我愿意为序列化创建一个新模型。
在按字段排序时,Django似乎是一个想法。
我需要对两个查询(queryset)进行合并,第一个查询被排名,第二个查询则没有,在最终结果中我想要一个,queryset因为它将被分页。
我将为您提供使用该User模型的示例,以便您可以在家中尝试。
from django.contrib.auth.models import User
from django.db.models import F, Value, IntegerField
from django.db.models.expressions import RawSQL
queryset = User.objects
a = queryset.filter(email__contains='a').annotate(rank=RawSQL("rank() OVER (ORDER BY id desc)", [], output_field=IntegerField()))
b = queryset.filter(email__contains='b').annotate(rank=Value(None, output_field=IntegerField()))
a.union(b).order_by(F('rank').desc(nulls_last=True))
# DatabaseError: ORDER BY term does not match any column in the result set.
a.order_by(F('rank').desc(nulls_last=True))
# this is OK
b.order_by(F('rank').desc(nulls_last=True))
# ProgrammingError: non-integer constant in ORDER BY
# LINE 1: ...ERE "auth_user"."email"::text LIKE '%b%' ORDER BY NULL DESC ... …Run Code Online (Sandbox Code Playgroud) 我需要检查一些网站的更新,我的脚本适用于我尝试的所有网站,除了一个.
我很确定它与TLS/SSL有关,但我没有找到任何关于错误的有用信息.
这是脚本:
package main
import (
"net/http"
"fmt"
"os"
"crypto/tls"
)
func main(){
client := &http.Client{
Transport: &http.Transport{
DisableKeepAlives: false,
MaxIdleConnsPerHost: 10,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
fmt.Println(client)
req, _ := http.NewRequest("GET", os.Args[1], nil)
_, err := client.Do(req)
if err != nil {
fmt.Println("Something bad happened: ", err)
}
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
[root@74dfbb491710 wd]# go run ssl.go https://google.com
&{0xc4200ce000 <nil> <nil> 0s}
[root@74dfbb491710 wd]# go run ssl.go https://jobs-eu.hudson.com
&{0xc4200e4000 <nil> <nil> 0s}
Something bad happened: Get https://jobs-eu.hudson.com: tls: …Run Code Online (Sandbox Code Playgroud)