小编Pao*_*olo的帖子

select_related with reverse foreign key

我在Django中有两个模型.第一个是工作职能(职位)报告到哪些其他职位的层次结构,第二个是人员和他们持有的工作职能.

class PositionHierarchy(model.Model):
    pcn = models.CharField(max_length=50)
    title = models.CharField(max_length=100)
    level = models.CharField(max_length=25)
    report_to = models.ForeignKey('PositionHierachy', null=True)


class Person(model.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    ...
    position = models.ForeignKey(PositionHierarchy)
Run Code Online (Sandbox Code Playgroud)

当我有人员记录并且我想找到该人的经理时,我必须这样做

manager = person.position.report_to.person_set.all()[0]
# Can't use .first() because we haven't upgraded to 1.6 yet
Run Code Online (Sandbox Code Playgroud)

如果我找到一个人QuerySet,我可以加入(并避免第二次访问数据库)使用position和report_to using Person.objects.select_related('position', 'position__reports_to').filter(...),但有没有办法避免再次访问数据库以获取person_set?我尝试添加'position__reports_to__person_set'或刚刚position__reports_to__personselect_related,但是这似乎并没有更改查询.这prefetch_related是为了什么?

我想创建一个自定义管理器,这样当我进行查询以获取Person记录时,我也会获得他们的PositionHeirarchy和他们的经理的Person记录,而不会再往返数据库.这是我到目前为止:

class PersonWithManagerManager(models.Manager):
    def get_query_set(self):
        qs = super(PersonWithManagerManager, self).get_query_set()
        return qs.select_related(
            'position',
            'position__reports_to',
        ).prefetch_related(
        )
Run Code Online (Sandbox Code Playgroud)

django django-models django-queryset django-managers django-1.5

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

有没有办法在tar球中导出当前的hg存储库头

HI,

有没有办法在tar球中导出当前的hg存储库头?我不需要tar球中的所有hg元文件(例如history/diff).

mercurial export archive

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

如何在django中分离我的模型?

我正在尝试学习python/django.

现在,我将所有模型都放在models.py中

是否可以将我的模型分解,以便我可以在单独的模型文件夹中为每个模型创建一个文件,以便我可以执行以下操作:

〜/ myproject/myapp/models/user.py~/myproject/myapp/models/group.py

谢谢

python django split django-models

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

从virtualenv,pip freeze> requirements.txt给出垃圾的TONES!如何修剪它?

我正在学习本教程:http: //devcenter.heroku.com/articles/django

在某些时候,我倾向于做:

pip freeze > requirements.txt
Run Code Online (Sandbox Code Playgroud)

(来自virtualenv创建的python实例)

我得到了这个:

(venv)przemoli@ubuntu:~/Programowanie/hellodjango$ cat requirements.txt 
BeautifulSoup==3.2.0
Brlapi==0.5.5
CherryPy==3.1.2
ClientForm==0.2.10
Django==1.3
GnuPGInterface==0.3.2
PAM==0.4.2
PIL==1.1.7
Routes==1.12.3
Twisted-Core==11.0.0
Twisted-Names==11.0.0
Twisted-Web==11.0.0
WebOb==1.0.8
adium-theme-ubuntu==0.3.1
apt-xapian-index==0.44
apturl==0.5.1ubuntu1
chardet==2.0.1
command-not-found==0.2.44
configglue==1.0
cssutils==0.9.8a1
defer==1.0.2
distribute==0.6.19
django-tagging==0.3.1
dnspython==1.9.4
duplicity==0.6.15
gnome-app-install==0.4.7-nmu1ubuntu2
httplib2==0.7.2
jockey==0.9.4
keyring==0.6.2
launchpadlib==1.9.8
lazr.restfulclient==0.11.2
lazr.uri==1.0.2
louis==2.3.0
lxml==2.3
mechanize==0.1.11
nvidia-common==0.0.0
oauth==1.0.1
onboard==0.96.1
oneconf==0.2.6.7
papyon==0.5.5
pexpect==2.3
piston-mini-client==0.6
protobuf==2.4.0a
psycopg2==2.4.4
pyOpenSSL==0.12
pycrypto==2.3
pycups==1.9.59
pycurl==7.19.0
pyinotify==0.9.1
pyparsing==1.5.2
pyserial==2.5
pysmbc==1.0.10
python-apt==0.8.0ubuntu9
python-dateutil==1.4.1
python-debian==0.1.20ubuntu2
python-virtkey==0.60.0
pyxdg==0.19
sessioninstaller==0.0.0
simplejson==2.1.6
system-service==0.1.6
ubuntu-sso-client==1.4.0
ubuntuone-couch==0.3.0
ubuntuone-installer==2.0.0
ubuntuone-storage-protocol==2.0.0
ufw==0.30.1-2ubuntu1
unattended-upgrades==0.1 …
Run Code Online (Sandbox Code Playgroud)

deployment django pip heroku virtualenv

29
推荐指数
3
解决办法
4万
查看次数

android模拟器中的console.log浏览器

如何console.log使用Android模拟器查看网站的消息?

android-emulator console.log

26
推荐指数
4
解决办法
3万
查看次数

Bootstrap版本和浏览器兼容性

我们目前正在开发一个必须在仍安装了IE6的旧公司PC上运行的应用程序.

我一直在寻找一下,但我找不到以下的明确答案:是否有一个链接,其中不同的引导版本与相应支持的浏览器版本配对?

例如:

Bootstrap 1.1 supports IE 6 and up, Chrome, FF, ...
Bootstrap 2.1 supports IE 7 and up, Chrome, FF, ...
Run Code Online (Sandbox Code Playgroud)

cross-browser internet-explorer-6 twitter-bootstrap

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

Vuejs无法从组件访问引用

我正在尝试获取组件模板内的canvas元素,为vuejs1找到了很棒的文档但不是vuejs2,其中"ref"是获取元素的唯一方法.我正在获取对象,但是当我尝试访问变量时,它是未定义的.

我的HTML

<div id="app>
  <template id="image-capture">
    <div class="row" >
      <canvas ref="icanvas" ></canvas>
    </div>
  </template>
</div>
Run Code Online (Sandbox Code Playgroud)

我的js

const ic = {
  template: '#image-capture' ,

  created () {
    console.log(this.$refs); //this returns object
    console.log(this.$refs.icanvas); // but this is undefined
  }


}

const routes = [
  { path: '/ic', component:   ic},
]

const router = new VueRouter({
  routes 
})

 new Vue({
  router,
   }).$mount('#app')
Run Code Online (Sandbox Code Playgroud)

我需要获得icanvas元素.

这是控制台日志

vue.js vue-component vuejs2

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

如何从select2()下拉列表Jquery中禁用所选属性?

我知道如何从下拉列表中启用所选属性; 我可以使用这段代码:

$('select').select2();
Run Code Online (Sandbox Code Playgroud)

但我的问题是如何禁用它?谢谢 点击这里

attributes selected jquery-select2

25
推荐指数
6
解决办法
9万
查看次数

如何从django模板生成静态html文件?

我是Django的新手,我很确定我已经阅读或听说过这样做的方法,但我无法在任何地方找到它.

我不想将渲染的输出从模板发送到浏览器,而是创建一个html文件,然后可以在不需要每次都进行渲染过程的情况下进行提供.我正在开发一个独立于我们主网站服务器的系统,我需要定期为我们的用户提供我的数据快照,而无需他们访问开发系统.

我的直觉说我应该能够以某种方式将响应重定向到文件,但我没有在文档或其他帖子中看到它.

html django django-templates static-html static-site

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

什么是 chunk-vendors.js 文件,它是如何创建的?(网络包)

我有一个关于在chunk-vendors.jsVue Js 应用程序的构建过程中创建的文件的快速问题。

它是什么?它是如何创建的?

我问的原因是为了更好地理解某些事情是如何结束的。我发现它实际上有一些我不想要的东西。

webpack vue-cli-3

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