开发者

how to enforce that object is only with a boolean attr set to true? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Unique BooleanField value in Django?

on a photograph model i want to choose it as the photograph that is the cover image for a gallery on a page that lists multiple galleries.

i wrote the following code, but it looks like ive created a loop

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.sav开发者_如何学Goe()

whats heppening is

  • upload an image, set it to portfolio_image
  • code runs through and hits the if statement
  • populates others, gets to o.save() and ends up running exactly the same code as i've defined.

looped!

how do i get round this?


I think you should store testimonial_image somewhere else, and it should equal id of needed Photograph object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜