17 views
Skip to first unread message

Aamu Padi

unread,
Nov 3, 2013, 3:23:44 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!

Lee Hinde

unread,
Nov 3, 2013, 4:18:11 PM11/3/13
to django...@googlegroups.com
I recently needed a similar thing (one item out of a table being _the_ thing) and decided that host table was the wrong place to store it. It needs to be in the other table, in this case, your user table should have a field that is the key of the profile picture.



Aamu Padi

unread,
Nov 3, 2013, 5:43:12 PM11/3/13
to django...@googlegroups.com
Thank you for the reply Lee. But could please elaborate a little more. :)





--
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/D75EAE65-8A05-4E92-9638-DD89D852800F%40gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

Lee Hinde

unread,
Nov 3, 2013, 6:27:09 PM11/3/13
to django...@googlegroups.com
You want there to be only one BackgroundImage record marked as 'using_image' for a given user. I'm saying you should add a field to your User table that tracks their BackgroundImage.

Rafael E. Ferrero

unread,
Nov 4, 2013, 6:04:52 AM11/4/13
to django...@googlegroups.com
I think that if u take a background image like a User Property then u must to extend User Model then make a one to one relationship from that to a with backgrounds model. With this your extended user model will have only one field who can take just one value... the ID of a Background Image.



2013/11/3 Lee Hinde <leeh...@gmail.com>

--
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.

For more options, visit https://groups.google.com/groups/opt_out.



--
Rafael E. Ferrero

Tom Evans

unread,
Nov 4, 2013, 7:52:50 AM11/4/13
to django...@googlegroups.com
User can have many images, but only one profile image:

class User:
profile_image = ForeignKey('BackgroundImage', blank=True, null=True)
# other fields

class BackgroundImage:
owner = ForeignKey('User')
# other fields

# get a profile pic
image_url = user.profile_pic and user.profile_pic.image.url or ''

# set a new profile pic
image = BackgroundImage(....)
user.profile_pic = image
user.save()

User can have many images, but only one profile image, without
modifying user model

class User:
# other fields

class BackgroundImage:
owner = ForeignKey('User')
# other fields

class UserBackgroundImage:
user = OneToOneField('User')
profile_picture = ForeignKey('BackgroundImage')

# get a profile pic
image_url = user.userbackgroundimage and
user.backgroundimage.profile_picture.image.url or ''

# set a new profile pic
image = BackgroundImage(....)
user_image, created =
UserBackgroundImage.objects.get_or_create(user=user,
defaults={'profile_picture': image})
if not created:
user_image.profile_picture = image
user_image.save()

Cheers

Tom

Aamu Padi

unread,
Nov 5, 2013, 4:39:20 PM11/5/13
to django...@googlegroups.com
Thank you all for the precious ideas! I made some changes and just about to get the result. :D
Thank you again!


--
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.
Reply all
Reply to author
Forward
0 new messages