【发布时间】:2011-08-29 15:56:28
【问题描述】:
在照片模型上,我想选择它作为列出多个画廊的页面上画廊封面图像的照片。
我写了以下代码,但看起来我创建了一个循环
class Photograph(models.Model):
project = models.ForeignKey(Project)
description = models.TextField(max_length=5000, default=False)
image = models.ImageField(upload_to="photographs/")
thumb = models.ImageField(upload_to="photographs/", blank=True, help_text="Ignore this field, it will autopopulate")
thumbnail = models.CharField(max_length=250, blank=True)
filename = models.CharField(max_length=100, default=False, help_text="Ignore this field, it will autopopulate")
portfolio_image = models.BooleanField(default=False, help_text="Do you want this image to appear as the portfolio image?")
date_added = models.DateTimeField('date published')
def save(self):
# get filename for use later
filename, extension = os.path.splitext(self.image.name)
self.filename = slugify(filename)
self.thumbnail = filename+"-t.jpg"
# is this the testimonial image, if so, unselect other images
if self.testimonial_image is True:
others = Photograph.objects.filter(project=self.project).filter(testimonial_image=True)
pdb.set_trace()
for o in others:
o.testimonial_image = False
o.save()
发生了什么
- 上传图片,设置为 投资组合图片
- 代码运行并命中 if 语句
- 填充其他人,到达 o.save() 并最终以完全相同的方式运行 我定义的代码。
循环!
如何解决这个问题?
【问题讨论】:
-
testimonial_image定义在哪里? -
另外,您应该在模型中将
thumb和filename设置为editable=False,或者将它们从您的表单中排除,而不是向用户说明它们将被自动填充 -
它们用于测试目的。现在要编辑问题,删除错误的问题,我得到了这个模型的推荐和组合图像
标签: django django-models