小编dja*_*aut的帖子

如何在django-rest-framework视图集中获取自定义列表视图

我有一个电视频道模型并创建了一个django-restframework viewlet,它为我提供了一个开箱即用的列表和详细视图.在顶部,我添加了两个名为all_events和now_and_next_event的自定义单对象视图,如下所述:标记额外的路由方法.到目前为止,这很有效.

class ChannelViewSet(viewsets.ModelViewSet):
    """
    A viewset for viewing and editing channel instances.
    """
    serializer_class = serializers.ChannelSerializer
    queryset = Channel.objects.all()

    @link()
    def now_and_next_event(self, request, pk):
        ''' Show current and next event of single channel. '''
        ...
Run Code Online (Sandbox Code Playgroud)

现在我想添加一个自定义视图,它不是单个对象视图,而是类似列表的视图:

class CurrentEvents(generics.ListCreateAPIView):
    ''' Show current event of all channels. '''
    model = Event
    serializer_class = serializers.EventSerializer

    def get(self, request):
        ...
Run Code Online (Sandbox Code Playgroud)

当我禁用我的viewlet并为其添加手动url模式时,它也可以正常工作.但我还没弄明白如何让它们都使用相同的'api/channel /'前缀,或者我想要的更多,如何将自定义列表视图类添加到我的viewlet中.

这是我的viewlet网址模式:

^api/channel/$ [name='channel-list']
^api/channel/(?P<pk>[^/]+)/$ [name='channel-detail']
^api/channel/(?P<pk>[^/]+)/all_events/$ [name='channel-all-events']
^api/channel/(?P<pk>[^/]+)/now_and_next_event/$ [name='channel-now-and-next-event']
Run Code Online (Sandbox Code Playgroud)

我想访问我的列表视图,如:

^api/channel/current_events/$ [name='event-current']
Run Code Online (Sandbox Code Playgroud)

django django-rest-framework

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

如何在vim中不移动光标的情况下向左/向右移动屏幕或将其水平居中?

例如,在线粘贴后,我的窗口显示文档的最右侧部分(列 >80)。

将光标留在当前位置后,如何将屏幕水平居中围绕它。或者,我可以将屏幕向左移动半个屏幕宽度,避免使用固定数字,例如44zh

虽然标题不具体,但所有答案都只涉及向上/向下移动: 如何在 Vim 中移动屏幕而不移动光标?

vim

9
推荐指数
3
解决办法
3048
查看次数

我可以让git add -p显示更改,例如git diff --word-diff吗?

我在代码库中递归替换了许多单字模式.在提交之前,我需要检查每个是否有错误的替换.这将帮助我很多有git add -p使用什么样的格式--word-diff与使用git diff,其中只有改变的话被标记,而不是整条生产线.

有人在这里问了同样的问题,但也许从那时起就实施了? https://groups.google.com/forum/#!topic/git-users/7uO2OUJGvP0

git git-diff git-patch

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

Gunicorn不会从nginx记录真正的ip

我通过gunicorn,supervisor和nginx作为反向代理运行django应用程序并努力让我的gunicorn访问日志显示实际的ip而不是127.0.0.1:

日志条目目前看起来像这样:

127.0.0.1 - - [09/Sep/2014:15:46:52] "GET /admin/ HTTP/1.0" ...
Run Code Online (Sandbox Code Playgroud)

supervisord.conf

[program:gunicorn]
command=/opt/middleware/bin/gunicorn --chdir /opt/middleware -c /opt/middleware/gunicorn_conf.py middleware.wsgi:application 
stdout_logfile=/var/log/middleware/gunicorn.log
Run Code Online (Sandbox Code Playgroud)

gunicorn_conf.py

#!python
from os import environ
from gevent import monkey
import multiprocessing
monkey.patch_all()

bind = "0.0.0.0:9000"
x_forwarded_for_header = "X-Real-IP"
policy_server = False
worker_class = "socketio.sgunicorn.GeventSocketIOWorker"   
accesslog = '-'
Run Code Online (Sandbox Code Playgroud)

我的nginx模块conf

server {
    listen 80;
    root /opt/middleware;
    index index.html index.htm;
    client_max_body_size 200M;
    server_name _;

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_redirect off;
        real_ip_header X-Real-IP;

      }
    }
Run Code Online (Sandbox Code Playgroud)

我在location …

django nginx supervisord gunicorn

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

如何高效查找源代码文件中的小错别字?

我想递归搜索大型代码库(主要是 python、HTML 和 javascript)以查找注释、字符串以及变量/方法/类名称中的拼写错误。对在终端中运行的东西的强烈偏好。

问题是拼写检查器喜欢aspellscspell几乎只发现误报(例如编程术语,驼峰式术语),而如果它可以帮助我主要找到简单的拼写错误,例如打乱或丢失的字母,例如维护与维护,限制与限制,我会很高兴,部署与部署。

到目前为止我在玩的是:

for f in **/*.py ; do echo $f ; aspell list < $f |  uniq -c ; done
Run Code Online (Sandbox Code Playgroud)

但它会找到类似的东西: assertEqual, MyTestCase, lifecycle

python lint spell-checking aspell

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

Django 1.11:在我的并行测试运行中“pickle”失败是什么?

我使用的是 dockerized Django 1.11,现在想并行运行我长期存在的测试套件:

manage.py test mainfolder --parallel -v2

尽管它在我的测试套件中 2 分钟后失败(到目前为止所有运行测试都成功),并出现 pickle 错误。然而,堆栈跟踪并没有告诉我它发生在哪里。我怎样才能更接近失败的地方呢? tblib已安装。

test_foo (myapp.some.testmodule.SomeTestCase) ... ok
Traceback (most recent call last):
  File "./myapp/manage.py", line 34, in <module>
    execute_from_command_line()
  File "/var/venv-stable/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/var/venv-stable/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/var/venv-stable/lib/python2.7/site-packages/django/core/management/commands/test.py", line 29, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "/var/venv-stable/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/var/venv-stable/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/var/venv-stable/lib/python2.7/site-packages/django/core/management/commands/test.py", line 62, in handle
    failures = …
Run Code Online (Sandbox Code Playgroud)

python django unit-testing multiprocessing

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

django-tenant-schemas:如何使用单租户/没有子域

我开始在我的项目中使用django-tenant-schemas,所有数据表(用户等)完全分开.

我的要求是,我可以将私有服务器中的相同代码库部署到单个客户,也可以部署在多个云上.django-tenant-schemas的路由不支持.如果我只有一个租户,它仍然需要一个子域来进行数据库路由.

此外,对于使用runserver进行localhost开发,我现在需要在/ etc/hosts中添加一行以使其正常工作,如:

127.0.0.1       testtenant.localhost.com
Run Code Online (Sandbox Code Playgroud)

我怎么能绕过那个?

python django postgresql

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

Django URL反向:不可逆的正则表达式部分:'(?='

Django版本:1.5(主干)

我在网址格式A中使用了肯定的前瞻性断言,它本身可以正常工作。但是当我尝试反向完全不相关的网址模式B时,我得到:

ValueError: Non-reversible reg-exp portion: '(?='
Run Code Online (Sandbox Code Playgroud)

网址示例:

url(r'^foo(?=bar)/', test, name= 'bla'),
url(r'bar/', test, name= 'bli'),
Run Code Online (Sandbox Code Playgroud)

触发错误:

from django.core.urlresolvers import reverse
reverse('bli')
Run Code Online (Sandbox Code Playgroud)

我找到了这张相关的票证,但可悲的是并没有使我更聪明 https://code.djangoproject.com/ticket/17492

任何人都可以告诉我代码有什么问题吗?

django django-urls

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