我想澄清django-rest-framework关于创建模型对象的给定文档.到目前为止,我发现有3种方法可以处理此类事件.
Serializer的create()方法.这是文档
class CommentSerializer(serializers.Serializer):
def create(self, validated_data):
return Comment.objects.create(**validated_data)
Run Code Online (Sandbox Code Playgroud)ModelViewset create()方法.文档
class AccountViewSet(viewsets.ModelViewSet):
queryset = Account.objects.all()
serializer_class = AccountSerializer
permission_classes = [IsAccountAdminOrReadOnly]
Run Code Online (Sandbox Code Playgroud)ModelViewset perform_create()方法.文档
class SnippetViewSet(viewsets.ModelViewSet):
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
Run Code Online (Sandbox Code Playgroud)根据您的应用程序环境,这三种方法很重要.
但是我们什么时候需要使用每个create() / perform_create()函数?在另一方面,我发现一些帐户,两个创建方法被调用单个post请求模型集create()和序列化程序create().
希望任何人都能分享他们的一些知识来解释,这肯定会对我的开发过程非常有帮助.
我有一个Employee定义如下的类:
Employee
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime WorkDate { get; set; }
public bool isOff { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的类实现和用法:
List<Employee> workers = new List<Employee>()
{
new Employee { Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/11/2016"), IsOff = false},
new Employee { Id = 1, Name = "Emp 1", WorkDate = Convert.ToDateTime("4/12/2016"), IsOff = false},
new Employee { Id = 1, Name …Run Code Online (Sandbox Code Playgroud) 我在点击mat-nav-list项目时尝试触发打开菜单.
HTML
<mat-nav-list>
<mat-list-item (click)="onOpenMenu(menu)" *ngFor="let i of data">
<div mat-line>
{{ i.name }}
</div>
<p mat-line>
{{ i.email }}
</p>
<button mat-icon-button [matMenuTriggerFor]="menu">
<mat-icon>more_vert</mat-icon>
</button>
</mat-list-item>
<mat-menu #menu="matMenu">
<button mat-menu-item>View profile</button>
<button mat-menu-item>Add contact</button>
</mat-menu>
</mat-nav-list>
Run Code Online (Sandbox Code Playgroud)
TS
onOpenMenu(menu: any): void {
// menu doesn't have any openMenu() function
// which is of course not a trigger object but a menu itself.
console.log(menu);
}
Run Code Online (Sandbox Code Playgroud)
我一直试图在github上看这个问题,这更接近我的情况.但在我的情况下,我有一个动态的项目列表,我想每次点击打开一个菜单.
我编写了几行代码来进行实验并区分这两个:interface和abstract class。
我发现它们有相同的限制。
interface IPerson {
name: string;
talk(): void;
}
interface IVIP {
code: number;
}
abstract class Person {
abstract name: string;
abstract talk(): void;
}
class ManagerType1 extends Person {
// The error i get is that i need to implement the talk() method
// and name property from its base class.
}
class ManagerType2 implements IPerson {
// The error i get is that i need to implement the talk() method
// …Run Code Online (Sandbox Code Playgroud) 我可以理解每一个pool.query()都会花费一个连接,它会在结束时自动释放.基于这个关于github问题的评论.但是使用的嵌套查询pool.getConnection()呢?
pool.getConnection(function(err, connection) {
// First query
connection.query('query_1', function (error, results, fields) {
// Second query
connection.query('query_2', function (error, results, fields) {
// Release the connection
// DOES THIS ALSO RELEASE query_1?
connection.release();
if (error) throw error;
// you can't use connection any longer here..
});
});
});
Run Code Online (Sandbox Code Playgroud)
UPDATE
这是执行嵌套查询时使用事务的代码.
const pool = require('../config/db');
function create(request, response) {
try {
pool.getConnection(function(err, con) {
if (err) {
con.release();
throw err;
}
con.beginTransaction(function(t_err) {
if (t_err) …Run Code Online (Sandbox Code Playgroud) 有时我们正在创建具有许多输入控件的表单,这些控件使容器(例如div)能够显示垂直滚动条.
我将此表单定义为a,FormGroup并且每个输入都FormControl包含一个md-error模板.
如果在提交表单时触发了md-error ,则可以滚动并聚焦表单控件?
根据v1.9 上添加的这个文档,我们可以查询一个DateTimeField没有时间的日期。
例子是:
Entry.objects.filter(pub_date__date=datetime.date(2005, 1, 1))
Entry.objects.filter(pub_date__date__gt=datetime.date(2005, 1, 1))
Run Code Online (Sandbox Code Playgroud)
但这对我不起作用:
class MilkStorage(models.Model):
....
created_at = models.DateTimeField(null=False)
Run Code Online (Sandbox Code Playgroud)
用法
from datetime import date
MilkStorage.objects.filter(created_at__date=date.today())
Run Code Online (Sandbox Code Playgroud)
它返回一个空的查询集<QuerySet []>。
此查询仅适用于PostgreSQL? 我正在使用MySQL.
我对 docker 的了解非常有限,所以我来这里发布这个 celery Worker 服务不会以以下错误启动:
错误:对于 api_worker_1 无法启动服务工作线程:b'OCI 运行时创建失败:container_linux.go:348:启动容器进程导致“exec: \”worker\”:在 $PATH 中找不到可执行文件”:未知”
Docker文件
# web
FROM python:2.7
RUN apt-get update
RUN apt-get install -y swig
RUN apt-get install -y libssl1.0-dev
RUN pip install --upgrade pip
ADD . /app
WORKDIR /app
CMD ["python", "-u","app.py"]
Run Code Online (Sandbox Code Playgroud)
docker-compose.yml
version: '2'
services:
db:
image: postgres
restart: always
volumes:
- ./postgres-data:/var/lib/postgresql/data
ports:
- "5432:5432"
web:
restart: always
build: .
volumes:
- ./web:/data/web
- .:/app
command: python -u app.py
ports:
- "5000:5000"
rabbit:
hostname: rabbit
image: …Run Code Online (Sandbox Code Playgroud) 我目前在突变 enum 上很难Argument。
以下是我的代码Mutation:
class CreatePerson(graphene.Mutation):
foo = graphene.String()
def mutate(self, info, **kwargs):
return CreatePerson(foo='foo')
class Arguments:
enum_arg = graphene.Argument(graphene.Enum.from_enum(EnumArg))
Run Code Online (Sandbox Code Playgroud)
枚举类:
from enum import Enum
class EnumArg(Enum):
Baz = 0
Bar = 1
Spam = 2
Egg = 3
Run Code Online (Sandbox Code Playgroud)
使用POSTMAN命令:
{
"query": "mutation": {createPerson(enumArg=1) { foo }}
}
Run Code Online (Sandbox Code Playgroud)
但我最终得到了这个错误信息:
"message": "Argument \"enumArg\" has invalid value 1.
Expected type \"EnumArg\", found 1.",
Run Code Online (Sandbox Code Playgroud)
我也试过给人enumArg=\"Bar\"的createPerson突变和错误仍然存在。
angular ×2
django ×2
python ×2
typescript ×2
c# ×1
celery ×1
docker ×1
enumerable ×1
graphql ×1
ienumerable ×1
linq ×1
mysql ×1
node-mysql ×1
node.js ×1
oop ×1
orm ×1
python-2.7 ×1
typeorm ×1