小编luc*_*wxp的帖子

我应该使用@return self,this还是当前的类?

我有一个返回当前对象的方法,我该如何记录?

/**
 * set something
 *
 * @return this
 */
public function setSomething(){
            // ...
    return $this;
}
Run Code Online (Sandbox Code Playgroud)

或者我应该做的@return self还是@return Current_Class_Name

php documentation doc phpdoc

40
推荐指数
4
解决办法
1万
查看次数

检查Django中是否存在模型字段

这是我在models.py中提出的解决方案:

from django.db import models

@classmethod
def model_field_exists(cls, field):
    try:
        cls._meta.get_field(field)
        return True
    except models.FieldDoesNotExist:
        return False

models.Model.field_exists = model_field_exists
Run Code Online (Sandbox Code Playgroud)

并使用它像:

Post.field_exists('title') # > True or False
Run Code Online (Sandbox Code Playgroud)

问题来自外键,我的Post模型属于一个类别,此检查有效:

Post.field_exists('category') # > True
Run Code Online (Sandbox Code Playgroud)

但是这个没有:

Post.field_exists('category_id') # > False
Run Code Online (Sandbox Code Playgroud)

这是db中的实际字段名称,我需要像这样检查它.我怎么能在django做到这一点?

python django

16
推荐指数
5
解决办法
2万
查看次数

nginx - 将请求转发到另一个代理

所以,我有一个第三方代理(可能在鱿鱼下),它只接受来自我的一个 IP 的连接,但我需要能够从各种 IP 访问它。

所以我试图放置一个 nginx 来将请求转发到这个代理。我知道 nginx 可以转发这样的请求:

location / {
    proxy_pass http://$http_host$uri$is_args$args;
}
Run Code Online (Sandbox Code Playgroud)

如果我需要 nginx 将请求直接转发到目标站点,这将起作用,但我需要它首先将其传递给代理 X。我试过这个:

upstream myproxy {
   server X.X.X.X:8080;
}

location / {
   proxy_pass http://myproxy$uri$is_args$args; // also tried: http://myproxy$http_host$uri$is_args$args
}
Run Code Online (Sandbox Code Playgroud)

但是我得到“(104)对等方重置连接”。我猜是因为 nginx 是这样代理的:

GET /index.html HTTP/1.1
Host: www.targetdomain.com.br
Run Code Online (Sandbox Code Playgroud)

但我需要它来代理这样的:

GET http://www.targetdomain.com.br/index.html HTTP/1.1
Run Code Online (Sandbox Code Playgroud)

proxy nginx

6
推荐指数
1
解决办法
2万
查看次数

传递接受接口的函数的struct

我有以下代码:

package main


type MyInterface interface {
    Test()
}

type MyType struct {

}
func (m MyType) Test(){}

func AcceptInterface(i *MyInterface){
}

func main() {

    object := &MyType{}
    AcceptInterface(object)
}
Run Code Online (Sandbox Code Playgroud)

我期待这个工作,因为MyType实现了MyInterface,但我得到:

不能在AcceptInterface的参数中使用对象(类型*MyType)作为类型*MyInterface:*MyInterface是指向接口的指针,而不是接口

我尝试做类型断言:对象.(MyInterface),但这也不起作用.

我怎么能做到这一点?

go

6
推荐指数
1
解决办法
5694
查看次数

无法修改数组内的struct?

我刚刚开始学习golang,我有以下代码:

https://play.golang.org/p/OBsf9MRLD8

package main

import (
    "encoding/json"
    "os"
)

type ResourceUsage struct {
    Type string
}
type Node struct {
    Resources []ResourceUsage
}

func main(){
    encoder := json.NewEncoder(os.Stdout)
    nodes := make([]Node, 2)
    nodes[0] = Node{}
    nodes[1] = Node{}

    for _,n := range nodes {
        n.Resources = append(n.Resources, ResourceUsage{Type: "test"})
    }
    encoder.Encode(nodes)
}
Run Code Online (Sandbox Code Playgroud)

我希望它能打印出来

[{"Resources":[{"Type:"test"}]},{"Resources":[{"Type:"test"}]}]
Run Code Online (Sandbox Code Playgroud)

但相反,我得到:

[{"Resources":null},{"Resources":null}]
Run Code Online (Sandbox Code Playgroud)

如何实现预期的输出?

go

1
推荐指数
1
解决办法
176
查看次数

标签 统计

go ×2

django ×1

doc ×1

documentation ×1

nginx ×1

php ×1

phpdoc ×1

proxy ×1

python ×1