Tag: django models

Django的Orm,如何查看(或logging)执行的查询?

有没有办法可以打印Django ORM正在生成的查询? 说我执行以下语句: Model.objects.filter(name='test') 我怎样才能看到生成的SQL查询?

我如何限制Django的GenericForeignKey到模型列表?

有没有告诉Django的一种方式,有一个内容types的GenericForeignKey只能指向预定义列表中的模型? 例如,我有4个模型:A,B,C,D和一个包含GenericForeignKey的模型X. 我可以告诉X,GenericForeignKey只允许A和B吗?

如何导入CSV数据到Django模型

我有一些CSV数据,我想使用示例CSV数据导入到Django模型中: 1;"02-01-101101";"Worm Gear HRF 50";"Ratio 1 : 10";"input shaft, output shaft, direction A, color dark green"; 2;"02-01-101102";"Worm Gear HRF 50";"Ratio 1 : 20";"input shaft, output shaft, direction A, color dark green"; 3;"02-01-101103";"Worm Gear HRF 50";"Ratio 1 : 30";"input shaft, output shaft, direction A, color dark green"; 4;"02-01-101104";"Worm Gear HRF 50";"Ratio 1 : 40";"input shaft, output […]

Django:如何防止数据库条目的同时修改

如果有防止两个或多个用户同时修改相同数据库条目的方法? 向执行第二次提交/保存操作的用户显示错误消息是可以接受的,但数据不应该被默默覆盖。 我认为locking条目不是一个选项,因为用户可能会使用“返回”button或简单地closures他的浏览器,永远留下锁。

Django:保存模型时填充用户标识

我有一个与被链接到标准Django用户模型的created_by字段的模型。 保存模型时,我需要自动填充当前用户的ID。 我不能在pipe理层做到这一点,因为网站的大部分地方不会使用内置的pipe理员。 任何人都可以build议我应该如何去做呢?

Django聚合:两个域的乘法求和

我有一个这样的模型 class Task(models.Model): progress = models.PositiveIntegerField() estimated_days = models.PositiveIntegerField() 现在我想在数据库级别上进行计算Sum(progress * estimated_days) 。 使用Django聚合我可以有每个领域的总和,但不是领域的乘法的总和。

在Django中,如何使用dynamic字段查找过滤QuerySet?

给定一个类: from django.db import models class Person(models.Model): name = models.CharField(max_length=20) 是否有可能,如果是这样,有一个基于dynamic参数的filter的QuerySet? 例如: # Instead of: Person.objects.filter(name__startswith='B') # … and: Person.objects.filter(name__endswith='B') # … is there some way, given: filter_by = '{0}__{1}'.format('name', 'startswith') filter_value = 'B' # … that you can run the equivalent of this? Person.objects.filter(filter_by=filter_value) # … which will throw an exception, since `filter_by` is […]

Django:从图像url中的ImageField添加图像

请原谅我难看的英语;-) 想象一下这个非常简单的模型: class Photo(models.Model): image = models.ImageField('Label', upload_to='path/') 我想从图片url创build一个照片(即,不要在djangopipe理站点手动)。 我认为我需要做这样的事情: from myapp.models import Photo import urllib img_url = 'http://www.site.com/image.jpg' img = urllib.urlopen(img_url) # Here I need to retrieve the image (as the same way that if I put it in an input from admin site) photo = Photo.objects.create(image=image) 我希望我能很好的解释这个问题,如果不告诉我的话。 谢谢 :) 编辑: 这可能工作,但我不知道如何将content转换为django文件: from urlparse […]

Django的auto_now和auto_now_add

对于Django 1.1。 我在我的models.py中有这个: class User(models.Model): created = models.DateTimeField(auto_now_add=True) modified = models.DateTimeField(auto_now=True) 更新一行时,我得到: [Sun Nov 15 02:18:12 2009] [error] /home/ptarjan/projects/twitter-meme/django/db/backends/mysql/base.py:84: Warning: Column 'created' cannot be null [Sun Nov 15 02:18:12 2009] [error] return self.cursor.execute(query, args) 我的数据库的相关部分是: `created` datetime NOT NULL, `modified` datetime NOT NULL, 这是值得担忧的吗? 侧面的问题:在我的pipe理工具中,这两个字段没有显示出来。 这是预期的吗?

Django的FileField与upload_to在运行时确定

我试图设置我的上传,所以如果用户joe上传文件,它将进入MEDIA_ROOT /乔,而不是让每个人的文件进入MEDIA_ROOT。 问题是我不知道如何在模型中定义这个。 这是目前的样子: class Content(models.Model): name = models.CharField(max_length=200) user = models.ForeignKey(User) file = models.FileField(upload_to='.') 所以我想要的是“而不是”。 作为upload_to,让它成为用户的名字。 我知道,从Django 1.0开始,你可以定义你自己的函数来处理upload_to,但是这个函数不知道用户是谁,所以我有点迷路了。 谢谢您的帮助!