小编Dom*_*och的帖子

nginx uwsgi websockets 502 Bad Gateway上游过早关闭连接,同时从上游读取响应头

我已经在这个问题上敲打了好几天,终于到了一堵砖墙.

我一直试图让我的堆栈运行:

http://django-websocket-redis.readthedocs.org/en/latest/running.html#django-with-websockets-for-redis-behind-nginx-using-uwsgi

我一直在看其他一些SO这样的文章:

nginx - uWSGI HTTP + websocket配置

他们似乎遇到了类似的问题,但解决方案对我不起作用.

基本上,每当我尝试启动我的uWSGI进程时,我都会遇到nginx 502坏网关屏幕.根据文档中的说明,我有两个单独的uwsgi进程在运行.

当我运行websocket uwsgi实例时,我得到以下内容:

*** running gevent loop engine [addr:0x487690] ***
[2015-05-27 00:45:34,119 wsgi_server] DEBUG: Subscribed to channels: subscribe-broadcast, publish-broadcast
Run Code Online (Sandbox Code Playgroud)

这告诉我,uwsgi实例运行正常.然后我运行我的下一个uwsgi进程,没有错误记录那里...

当我导航到浏览器中的页面时,该页面会挂起几秒钟,然后才能获得502 Bad Gateway屏幕.

根据NGINX日志,NGINX说:

2015/05/26 22:46:08 [error] 18044#0: *3855 upstream prematurely closed connection while reading response header from upstream, client: 192.168.59.3, server: , request: "GET /chat/ HTTP/1.1", upstream: "uwsgi://unix:/opt/django/django.sock:", host: "192.168.59.103:32768"
Run Code Online (Sandbox Code Playgroud)

这是我尝试在Web浏览器中访问该页面时获得的唯一错误日志.

任何人的想法???

以下是我的一些配置文件:


nginx.conf

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 768;
}

http { …
Run Code Online (Sandbox Code Playgroud)

django nginx websocket redis uwsgi

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

如何过滤嵌套的相关 django 对象

我有一个应用程序,其中有很多投资者投资相同的轮次,这些轮次都属于公司,如下所示。但是,当用户(投资者)登录时,我只希望他能够看到他的投资。

    {
        "id": 1,
        "name": "Technology Company",
        "rounds": [
            {
                "id": 1,
                "kind": "priced round",
                "company": 1,
                "investments": [
                    {
                        "id": 1,
                        "investor": 1,
                        "round": 1,
                        "size": 118000,
                    },
                    {
                        "id": 2,
                        "investor": 2,
                        "round": 1,
                        "size": 183000,
                    },
                ]
            }
        ]
    },
Run Code Online (Sandbox Code Playgroud)

目前,我的视图集扩展get_queryset如下:

class CompanyViewSet(viewsets.ModelViewSet):
    def get_queryset(self):
        user = self.request.user
        investor = Investor.objects.get(user=user)
        companies = Company.objects.filter(rounds__investments__investor=investor)
        return companies
Run Code Online (Sandbox Code Playgroud)

它检索属于投资者的投资,但是当它再次使用这些投资来检索轮次时,它会与所有投资者一起抢占该轮次。

我怎么写才能让它只向投资者显示下面的投资?

这是我的模型:

class Company(models.Model):
    name = models.CharField(max_length=100)

class Round(PolymorphicModel):
    company = models.ForeignKey(Company, related_name='rounds', blank=True, null=True)

class Investment(PolymorphicModel):
    investor …
Run Code Online (Sandbox Code Playgroud)

django django-models django-rest-framework

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

在 Nuxt Auth 中设置会话 ID Cookie

我在我的 nuxt.config.js 文件中设置了以下内容:

auth: {
redirect: {
  login: '/accounts/login',
  logout: '/',
  callback: '/accounts/login',
  home: '/'
},
strategies: {
  local: {
    endpoints: {
      login: { url: 'http://localhost:8000/api/login2/', method: 'post' },
      user: {url: 'http://localhost:8000/api/user/', method: 'get', propertyName: 'user' },
      tokenRequired: false,
      tokenType: false
    }
  }
},
localStorage: false,
cookie: true
},
Run Code Online (Sandbox Code Playgroud)

我在身份验证后端使用 django 会话,这意味着成功登录后,我将在响应 cookie 中收到会话 ID。但是,当我使用 nuxt 进行身份验证时,我在响应中看到了 cookie,但未保存 cookie 以供进一步请求使用。知道我还需要做什么吗?

django nuxt.js

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

Django REST Serializer做N + 1数据库调用多个嵌套关系,3个级别

我有一种情况,我的模型有外键关系:

# models.py
class Child(models.Model):
    parent = models.ForeignKey(Parent,)

class Parent(models.Model):
    pass
Run Code Online (Sandbox Code Playgroud)

和我的序列化器:

class ParentSerializer(serializer.ModelSerializer):
    child = serializers.SerializerMethodField('get_children_ordered')

    def get_children_ordered(self, parent):
        queryset = Child.objects.filter(parent=parent).select_related('parent')
        serialized_data = ChildSerializer(queryset, many=True, read_only=True, context=self.context)
        return serialized_data.data

    class Meta:
        model = Parent
Run Code Online (Sandbox Code Playgroud)

当我在N个父母的视图中调用Parent时,Django在抓取子节点时在序列化器内进行N次数据库调用.有没有办法让所有的孩子为所有家长最小化数据库呼叫的数量?

我试过这个,但它似乎没有解决我的问题:

class ParentList(generics.ListAPIView):

    def get_queryset(self):
        queryset = Parent.objects.prefetch_related('child')
        return queryset

    serializer_class = ParentSerializer
    permission_classes = (permissions.IsAuthenticated,)
Run Code Online (Sandbox Code Playgroud)

编辑

我已经更新了下面的代码以反映Alex的反馈....它解决了一个嵌套关系的N + 1.

# serializer.py
class ParentSerializer(serializer.ModelSerializer):
    child = serializers.SerializerMethodField('get_children_ordered')

    def get_children_ordered(self, parent):
        # The all() call should hit the cache
        serialized_data = ChildSerializer(parent.child.all(), many=True, read_only=True, …
Run Code Online (Sandbox Code Playgroud)

django polymorphism django-models django-rest-framework

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