class Book(models.Model):
title = models.CharField(..., null=True)
type = models.CharField(...)
author = models.CharField(...)
Run Code Online (Sandbox Code Playgroud)
我在models.py中有一个简单的类.在管理员中,我希望在保存的书的类型为1时隐藏书的标题(在书的详细信息表格中).如何以最简单的方式进行?
我正在尝试使用django-pipeline来缩小静态资源,为它们使用缓存并使我的模板更简单.我的浏览器找到并加载了我的CSS和JS文件,但我的(非常简单的)主页加载大约需要10秒钟.

我使用的是Python 2.7.6,Django 1.7.3和django-pipeline 1.4.3.PyCharm使用本地virtualenv运行开发服务器.
我的settings.py包含以下内容:
DEBUG = True
TEMPLATE_DEBUG = DEBUG
INSTALLED_APPS = (
'django_admin_bootstrapped', # custom admin
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# pip installed apps
'pipeline',
# project apps
'myapp',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'pipeline.middleware.MinifyHTMLMiddleware',
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'pipeline.finders.FileSystemFinder',
'pipeline.finders.CachedFileFinder',
'pipeline.finders.PipelineFinder',
)
STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'myapp/static'),
)
STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'
PIPELINE_CSS_COMPRESSOR = 'pipeline.compressors.yuglify.YuglifyCompressor'
PIPELINE_JS_COMPRESSOR = …Run Code Online (Sandbox Code Playgroud) 我已经使用Django几年了但是我今天在HAVING为a 添加约束而苦苦挣扎GROUP BY.
我的查询集如下:
crm_models.Contact.objects\
.filter(dealercontact__dealer__pk__in=(265,),
dealercontact__activity='gardening',
date_data_collected__gte=datetime.date(2012,10,1),
date_data_collected__lt=datetime.date(2013,10,1))\
.annotate(nb_rels=Count('dealercontact'))
Run Code Online (Sandbox Code Playgroud)
这给了我以下MySQL查询:
SELECT *
FROM `contact`
LEFT OUTER JOIN `dealer_contact` ON (`contact`.`id_contact` = `dealer_contact`.`id_contact`)
WHERE (`dealer_contact`.`active` = True
AND `dealer_contact`.`activity` = 'gardening'
AND `contact`.`date_data_collected` >= '2012-10-01'
AND `contact`.`date_data_collected` < '2013-10-01'
AND `dealer_contact`.`id_dealer` IN (265))
GROUP BY `contact`.`id_contact`
ORDER BY NULL;
Run Code Online (Sandbox Code Playgroud)
我会得到这个HAVING约束我需要的东西:
HAVING SUM(IF(`dealer_contact`.`type`='customer', 1, 0)) = 0
Run Code Online (Sandbox Code Playgroud)
如何使用Django Queryset修复此问题?我在这个实例中需要一个查询集.
这里我只使用注释才能GROUP BY开启contact.id_contact.
编辑:我的目标是获得在dealercontact中没有"客户"关系但具有"ref"关系的联系人(当然根据WHERE子句).
class Contact(models.Model):
id_contact = models.AutoField(primary_key=True)
title = …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下Model和ModelAdmin类将搜索添加到我的模型管理列表页面:
models.py
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.OneToOneField(User)
country = CountryField(blank=True, null=True)
Run Code Online (Sandbox Code Playgroud)
admin.py
from django.contrib import admin
from models import UserProfile
class UserProfileAdmin(admin.ModelAdmin):
list_display = ('user','country')
search_fields = ['user']
Run Code Online (Sandbox Code Playgroud)
但是在尝试访问管理面板中的UserProfile时出现以下错误:
at /admin/profiles/userprofile/ Related Field has invalid
lookup: icontains
Run Code Online (Sandbox Code Playgroud)
我也尝试过以下方法:
search_fields = ['user_username']
Run Code Online (Sandbox Code Playgroud)
和
search_fields = ['user_name']
def user_name(self,obj):
return obj.user.username
Run Code Online (Sandbox Code Playgroud)
有解决方案吗
我正在使用一些 github django 包的前沿版本,这些版本具有我不想安装的依赖项。
在开发中,在我自己的计算机上,我可以pip install在命令行中使用并使用--no-dependencies标志。但是,我的测试和生产环境需要一个用于部署目的的需求文件。不幸的是,该--no-dependencies标志不能在需求文件中使用,如下所述:https : //pip.pypa.io/en/latest/reference/pip_install.html#requirements-file-format。
有没有办法告诉 pip 在使用需求文件时不要安装依赖项?