我有一个返回当前对象的方法,我该如何记录?
/**
* set something
*
* @return this
*/
public function setSomething(){
// ...
return $this;
}
Run Code Online (Sandbox Code Playgroud)
或者我应该做的@return self
还是@return Current_Class_Name
?
这是我在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做到这一点?
所以,我有一个第三方代理(可能在鱿鱼下),它只接受来自我的一个 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) 我有以下代码:
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),但这也不起作用.
我怎么能做到这一点?
我刚刚开始学习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)
如何实现预期的输出?