Model to select only 1 object from multiple objects

30 views
Skip to first unread message

Aamu Padi

unread,
Nov 3, 2013, 3:27:14 PM11/3/13
to django...@googlegroups.com
How to make a model with a yes/no field, if yes that particular object (image) will be used as the background, and no other image will be selected.

I have come with this model:

    class BackgroundImage(models.Model):
        user = models.ForeignKey(user)
        caption = models.CharField(max_length=
200)
        image = models.ImageField(upload_to=get_upload_file_name)
        using_image = models.BooleanField()

But with this I can select all of them, and not only one.

Lets take an example of profile picture. A user can have many images, but he can only select one as a profile picture. So, that if he selects one, the other will be un-select itself.
Hope everybody understood what I mean. Please ask me if didn't understood. Also please correct me if my model is not correct. Thank you!

Ruturaj Dhekane

unread,
Nov 4, 2013, 9:13:59 AM11/4/13
to django...@googlegroups.com
Your model can work. But I think the model there is an alternate way to create this model.

The way I might design this is have a profile model
Profile = name, birth date, location, favorite books, friends, ProfilePicture (foreignKey)

ProfilePicture = image, caption
So now, you can have multiple profile pictures and the profile will point to that profile picture which needs to be shown. Hope this helps.

To do it the way you are doing - you need to make sure that first all User=X have set using_image=false. Then set the particular used using_image=true. Then you can retrieve the unique photo for every user by filtering on using_image = true This needs to be done in code rather than in model.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHSNPWtW89SBNRnCh_EO6Uj1hTPhixsxSRwq%3DhHmuGzdTJfWAg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Aamu Padi

unread,
Nov 4, 2013, 5:17:48 PM11/4/13
to django...@googlegroups.com
Thank you. I hope I can walk through now. :)


Reply all
Reply to author
Forward
0 new messages