[Answer]-Django – Model to chose only 1 object from multiple objects

1👍

You could add a save() hook to ensure no other BackgroundImage for the given user has the flag. (Also, you’ll want that column to be db_index=True for performance reasons.)

def save(self, *args, **kwargs):
    if self.use_image:
        BackgroundImage.objects.filter(user=self.user).update(use_image=False)
    super(BackgroundImage, self).save(*args, **kwargs)
👤AKX

Leave a comment