是否可以使用Django-rest-framework进行case-insensitive排序.first_name
这是代码:
import django_filter
class PersonFilter(django_filters.FilterSet):
class Meta:
model = Person
fields = ('first_name_lower',)
order_by = ('first_name_lower',)
class PersonViewSet(BaseModelViewSet):
queryset = Person.objects.all()
permission_classes = (permissions.IsAuthenticated,)
filter_backends = (filters.DjangoFilterBackend,)
filter_class = PersonFilter
Run Code Online (Sandbox Code Playgroud)
case-insensitive订购时有简单的方法django-filter吗?
这里 django-filter有不区分大小写搜索的文档,但没有用于排序.
在Django文档中,代码对此有点迟钝,这让我想知道它是否存在django-filter.这里是关于如何使用Django ORM的Django文档代码片段:
>>> from django.db.models.functions import Lower
>>> MyModel.objects.order_by(Lower('myfield'))
Run Code Online (Sandbox Code Playgroud) 有没有办法在 Django 中存储旧密码哈希值,这样用户就不能重复使用相同的密码?
在对此进行研究时,每次 Django 创建密码哈希时,即使密码相同,哈希也是不同的。例如,这将返回两个不同的哈希值:
from django.contrib.auth.hashers import make_password
make_password('foo')
make_password('foo')
Run Code Online (Sandbox Code Playgroud)
我可以理解这样做的安全原因。有人尝试在 Django 中这样做吗?以某种方式存储旧密码,以便用户在密码过期时必须使用新密码等......?
我正在尝试添加两个组并授予他们访问我的 Django 项目的权限。但我不断收到错误消息:
ContentType 匹配查询不存在。
我正在运行:Django 1.5.4 Python 2.7.3 South 0.8.2 PostreSQL 9.3
这是我的代码:
import django
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
from .models import Flavor
def add_groups():
# Create User Groups
special_users = Group(name='Special Users')
special_users.save()
really_special_users = Group(name='Super Special Users')
really_special_users.save()
def add_permissions():
# Define a View permission for the 1st group, and a View/Modify permission for the 2nd group
somemodel_ct = ContentType.objects.get(app_label='flavors', model='flavors_flavor')
can_view = Permission(name='Can View', codename='can_view_something', content_type=somemodel_ct)
can_view.save()
can_modify = Permission(name='Can Modify', …Run Code Online (Sandbox Code Playgroud) 你如何在AngularJs中模拟一个简单的Factory,它在Karma单元测试中返回静态数据?
我有这个简单的工厂,为了示例返回静态数据:
angular.module('conciergeApp.services')
.factory('CurrentUser', function() {
return {
id: 1,
hotel_id: 1,
}
});
Run Code Online (Sandbox Code Playgroud)
我想知道如何为此编写Karma测试?
到目前为止,我有这个代码,但id不起作用:
describe('ReplyCtrl', function() {
beforeEach(module('conciergeApp'));
beforeEach(module(function($provide) {
$provide.service('CurrentUser', function() {
return 1;
});
}));
//Getting reference of the mocked service
var mockUtilSvc;
inject(function(CurrentUser) {
mockUtilSvc = CurrentUser;
});
beforeEach(inject(function($rootScope, $controller) {
scope = $rootScope.$new();
ctrl = $controller('ReplyCtrl', {
$scope: scope
});
}));
it('should return value from mock dependency', inject(function(mockUtilSvc) {
expect(mockUtilSvc()).toEqual(1);
}));
});
Run Code Online (Sandbox Code Playgroud)
这是我收到的错误消息:
Firefox 41.0.0 (Mac OS X …Run Code Online (Sandbox Code Playgroud) 我试图在1个数据库中有选择地创建表,但不是另一个.
在我的例子,我只是想为应用程序创建表:tlocation和tcategory在transforms数据库中.但是,Django正在transforms数据库中创建所有表.
这是DB路由器配置:
TRANSFORM_APPS = ('tcategory', 'tlocation')
class TransformRouter(object):
"""
A router to control all database operations on models in the
"utils_transform" application.
"""
def db_for_read(self, model, **hints):
"""
Attempts to read 'transforms' models go to 'transforms' database.
"""
if model._meta.app_label in TRANSFORM_APPS:
return 'transforms'
return None
def db_for_write(self, model, **hints):
"""
Attempts to write 'transforms' models go to 'transforms' database.
"""
if model._meta.app_label in TRANSFORM_APPS:
return 'transforms'
return None
def allow_relation(self, …Run Code Online (Sandbox Code Playgroud) 我的models.py中有以下图像字段(参见下面的代码)
我想设置一个固定的图像宽度和高度,所以它总是100x100px
下面的代码是系统中已有的代码,我不确定如何传递宽度和高度,或者是否可以使用此代码将宽度和高度设置为固定大小.
image = models.ImageField(
upload_to="profiles",
height_field="image_height",
width_field="image_width",
null=True,
blank=True,
editable=True,
help_text="Profile Picture",
verbose_name="Profile Picture"
)
image_height = models.PositiveIntegerField(null=True, blank=True, editable=False, default="100")
image_width = models.PositiveIntegerField(null=True, blank=True, editable=False, default="100")
Run Code Online (Sandbox Code Playgroud) 我正在尝试Trip #为具有连续天数的客户分配一个记录,如果他们在连续几天中断,并且例如在本月晚些时候到达,则增加旅行ID.数据结构如下所示:
CustomerID Date
1 2014-01-01
1 2014-01-02
1 2014-01-04
2 2014-01-01
2 2014-01-05
2 2014-01-06
2 2014-01-08
Run Code Online (Sandbox Code Playgroud)
基于上述示例数据集的所需输出将是:
CustomerID Date Trip
1 2014-01-01 1
1 2014-01-02 1
1 2014-01-04 2
2 2014-01-01 1
2 2014-01-05 2
2 2014-01-06 2
2 2014-01-08 3
Run Code Online (Sandbox Code Playgroud)
因此,如果该客户的日期是背靠背,则它被认为是相同的旅行,并且具有相同的旅行#.有没有办法在SQL Server中执行此操作?我正在使用MSSQL 2012.
我最初的想法是使用LAG,ROW_NUMBER或OVER/PARTITION BY功能,甚至一个Recursive Table Variable Function.我可以粘贴一些代码,但说实话,我的代码到目前为止还没有.如果这是一个简单的查询,但我只是没有正确地考虑它,那将是很好的.
先感谢您.
是否有可能在Django Rest Framework 中在ModelViewSet 列表视图中的每条记录HyperLink的id字段上添加链接到该记录的详细信息视图?
该文件说,添加一个view_name说法,但是这是有可能下这样做ModelViewSet,因为看法实际上没有名字呢?
我试图让我的Apache服务器运行mod_wsgi并收到此错误:
[Fri Jul 19 10:24:05 2013] [warn] mod_wsgi: Compiled for Python/2.7.3.
[Fri Jul 19 10:24:05 2013] [warn] mod_wsgi: Runtime using Python/2.7.4.
Run Code Online (Sandbox Code Playgroud)
这是通过进入/usr/local/srcPython所在位置并运行来解决的:
./configure --enable-shared
make install
Run Code Online (Sandbox Code Playgroud)
到目前为止我所看到的似乎修复需要mod_wsgi必须能够访问所有Python库,因此运行--enable-shared命令将允许它工作.[warn]在我的Apache error.log中看到上面的行之后,它说它无法在我的DJANGO_SETTINGS_MODULE settings.py文件中查找环境变量.当我替换这些变量时,它会说它可以在其他地方查找其他Python包.
我在Rackspace VPS上运行Ubuntu Linux 13.04,使用Django 1.5.2,在我的本地机器上运行Python 2.7.3,但Rackspace VPS的本机Python是2.7.4.我目前使用的mod_wsgi版本是3.4
我找到了这个链接
http://code.google.com/p/modwsgi/wiki/InstallationIssues
在"Python补丁级别不匹配"一节中,它似乎说我必须重建mod_wsgi,但这听起来有点困难,所以想问一下是否有更简单的修复?
先感谢您,
我有以下简单的类,我想知道是否有使用一个简单的办法lambda,decorator或者helper method,等...避免重复的循环,在每个方法出现在CODENAMES和ALL_DEFAULTS?
class classproperty(object):
"""
When used to decorate a method in a class, that method will behave
like as a class property.
"""
def __init__(self, f):
# f - the func that's being decorated
self.f = f
def __get__(self, obj, cls):
# call the func on the class
return self.f(cls)
class PermissionInfo(object):
MODELS = ['ticket', 'person', 'role']
PERMS = ['view', 'add', 'change', 'delete']
@classproperty
def CODENAMES(cls):
codenames …Run Code Online (Sandbox Code Playgroud) 今天的日期或时间是否在模板中的默认Django上下文中可用?
我知道,我可以添加一个context_processor使django.utils.timezone.now模板全局可用.
我只是想知道它是否已经可用?如果是这样,名称是什么/如何访问它?这样我就不会创造已存在的东西.