我有一个表/模型调用Employees
,我想将单个字段的所有行作为查询集.
我知道我可以这样做(希望我能做到这一点):
emp_list = Employees.objects.get(all)
emp_names = emp_list.eng_name
Run Code Online (Sandbox Code Playgroud)
会在数据库中查询所有字段并只使用一个吗?这样做有更好(更快)的方法吗?
任何人都可以告诉我为什么在以下代码中我被重定向到yahoo.com而不是google.com?
网址
urlpatterns = patterns('', (r'^$', initialRequest,))
Run Code Online (Sandbox Code Playgroud)
视图
def initialRequest(request):
if request.user.is_authenticated:
return HttpResponseRedirect('http://yahoo.com')
else:
return HttpResponseRedirect('http://google.com')
Run Code Online (Sandbox Code Playgroud) 我发现当我form field
通过表单插入任何字符串时,它会将其捕获为空字符串''
.
但是,当我form field
通过表单插入整数时,它将其捕获为[null]
.
这是好习惯吗?字符串应该null
在数据库中也一样吗?
我试图将模板传递给切割过滤器,类似这样
{{ myVariable|cut:"something + templateVariable" }}
Run Code Online (Sandbox Code Playgroud)
我试过了:
{{ myVariable|cut:"something"|add:templateVariable }}
Run Code Online (Sandbox Code Playgroud)
和
{{ myVariable|cut:"something {{ templateVariable }}" }}
Run Code Online (Sandbox Code Playgroud)
但这些都行不通.
这可能吗?
我看到像亚马逊和谷歌这样的网站,当你缩小网络浏览器的宽度时,元素只会挤压到某一点,然后即使你缩小宽度也会停止压扁.
什么样的技术可以允许这个?是否有可以启用此功能的特定CSS属性?
我有一个模型,我想用搜索词过滤,这通常是一个名称.然而,在我的数据库first_name
和last_name
是两个不同的领域.
例如
search_term = 'Will Sm'
db_persons
记录: first_name = 'Will', last_name = 'Smith'
搜索词将能够检索此记录.
我怎样才能做到这一点?
db_persons.objects.filter(__?__)
更新:
寻找一种连接字段和查询连接结果而无需原始sql的方法
我有一个我循环的ArrayList,通过一些逻辑,我将删除特定索引处的元素.
然而,当我循环Arraylist并在途中删除时,ArrayList大小和特定项的索引也在变化,导致意外结果.
无论如何要绕过这个?
我有一个模型,我想在Django Admin中保存
class Product(models.Model):
# other fields
img1 = models.ImageField(upload_to='%s/%s/1/large/' % (category, prod_no))
img1_thumb = models.ImageField(upload_to='%s/%s/1/thumbnail/' % (category, prod_no), editable=False)
def save(self, *args, **kwargs):
newImg1 = resizeImg(self.img1, (75, 112))
self.img1_thumb = newImg1
super(Product, self).save(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
调整大小图像功能
def resizeImg(image, size):
try:
if imghdr.what(image) == 'jpeg':
img = Image.open(image)
img.thumbnail(size, Image.ANTIALIAS)
# this is how to save the img
# img.save(filename + '.jpg', 'JPEG', quality=75)
return img
else:
return 'not_jpg'
except Exception, e:
return 'exception'
Run Code Online (Sandbox Code Playgroud)
在Django Admin中保存此错误会产生此错误
AttributeError at /admin/myapp/product/add/
_committed
Run Code Online (Sandbox Code Playgroud)
更新 …