0

I'm looking to associate zero to three images to a class object when it is being created and I don't know the best way to do this.

Example: When a user creates a picture contest, they define their picture style with zero to three images. These are static images so they will never be updated.

The only way I can think of is to create a separate class called stylePictures and have three fields in the Contest saying "styleOne" "styleTwo" and "styleThree" which can be null. This seems like it is not the most efficient way to do this.

Any ideas?

1 Answer 1

2

I would create two separate classes. This way your feature can easily evolve into something that uses more pictures in the future.

class PictureContest(models.Model):
    # fields

class PictureStyle(models.Model):
    image = models.FileField(upload_to="upload/picture_style")
    picture_contest = models.ForeignKey("PictureContest")

To get all the picture styles associated with the one picture contest.

First you must isolate the picture contest that you want.

pc = PictureContest.objects.filter(id=2) # find your picture contest

Then filter the picture styles that have pc.id as a field value.

pic_styles = PictureStyle.objects.filter(picture_contest__id=pc.id)
Sign up to request clarification or add additional context in comments.

1 Comment

This is the route I was thinking, but then in PictureContest what fields would you add to say if a particular PictureStyle instance was chosen? I need to have zero to three PictureStyle objects associated with one PictureContest object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.