And how do I copy these images from one model to another?
On Wed, Nov 6, 2013 at 1:40 AM, Rafael E. Ferrero <[email protected]>wrote: > if you see many similarities between two models will surely are the same > thing. So BackgroundImage and ProfilePicture are the same thing and Photo > its very similar to. I think you must to use inheritance of models [1] an > use choices [2] for distinct types of pics > > I would do the following: > > class Pics(models.Model): > PICS_TYPES = ( > ('PHOTOS', 'Photos'), > ('BACKGROUNDS', 'Backgrounds'), > ('PROFILES', 'Profiles') > ) > user = models.ForeignKey(User) > image = models.ImageField(upload_to=get_upload_file_name) > caption = models.CharField(max_length=200) > pub_date = models.DateTimeField(default=datetime.now) > type = models.CharField(max_length=50, choices=PICS_TYPES) > > > class Album(models.Model): > user = models.ForeignKey(User) > name = models.CharField(max_length=200) > pub_date = models.DateTimeField(default=datetime.now) > > > class Photo(Pics): > album = models.ForeignKey(Album, default=3) > > > [1] > https://docs.djangoproject.com/en/1.5/topics/db/models/#model-inheritance > [2] > https://docs.djangoproject.com/en/1.5/ref/models/fields/#django.db.models.Field.choices > > > > 2013/11/5 Aamu Padi <[email protected]> > >> Each user has many *albums*, and each album has its many photos. And >> each user has one *background image* to hold its many images. Similarly, >> a user has one *profile picture* to hold its many images. >> >> These are my models: >> >> class UserProfile(models.Model): >> user = models.OneToOneField(User) >> permanent_address = models.TextField() >> temporary_address = models.TextField() >> profile_pic = models.ForeignKey(ProfilePicture) >> background_pic = models.ForeignKey(BackgroundImage) >> >> >> class Album(models.Model): >> user = models.ForeignKey(User) >> name = models.CharField(max_length=200) >> pub_date = models.DateTimeField(default=datetime.now) >> >> >> class Photo(models.Model): >> album = models.ForeignKey(Album, default=3) >> image = models.ImageField(upload_to=get_upload_file_name) >> caption = models.CharField(max_length=200) >> pub_date = models.DateTimeField(default=datetime.now) >> >> >> class BackgroundImage(models.Model): >> user = models.ForeignKey(User) >> image = models.ImageField(upload_to=get_upload_file_name) >> caption = models.CharField(max_length=200) >> pub_date = models.DateTimeField(default=datetime.now) >> >> >> class ProfilePicture(models.Model): >> user = models.ForeignKey(User) >> image = models.ImageField(upload_to=get_upload_file_name) >> caption = models.CharField(max_length=200) >> pub_date = models.DateTimeField(default=datetime.now) >> >> I tried doing this, to let the user copy one image from *ProfilePicture*to >> *Background >> Image*, but didn't work: >> >> # Get the user >> >>>m = User.objects.get(username='m') >> >> # Get its set of Profile pictures >> >>>m_pro_set = m.profilepicture_set.all() >> >>>m_pro_set >> [<ProfilePicture: pro_mik>] >> >> # Get its set of Background images >> >>>m_back_set = m.backgroundimage_set.all() >> >>>m_back_set >> [<BackgroundImage: bg_mik>] >> >> # Get an image object from Profile picture of the user >> >>>m_pro_1 = m.profilepicture_set.get(id=2) >> >>>m_pro_1 >> <ProfilePicture: pro_mik> >> >> # Get an image object from Background image of the user >> >>>m_back_1 = m.backgroundimage_set.get(id=2) >> >>>m_back_1 >> <BackgroundImage: bg_mik> >> >> # So, I tried to copy one image from *BackgroundImage* of a user *to* >> *ProfilePicture* >> >> >>>m_pro_set.objects.create(m_back_1) >> >> >> * File "<console>", line 1, object has attribute 'objects'* >> *AttributeError: 'QuerySet' object has no attribute 'objects'* >> >> So my question is, how to copy an objects from one model to another? Any >> advice will be much appreciated. Thank you! >> >> -- >> 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 [email protected]. >> To post to this group, send email to [email protected]. >> 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/CAHSNPWue7HtdGuJeqtEPsa%2Bncc2rrpE_tEuZSXRA4NXZtsmxkA%40mail.gmail.com >> . >> For more options, visit https://groups.google.com/groups/opt_out. >> > > > > -- > Rafael E. Ferrero > > -- > 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 [email protected]. > To post to this group, send email to [email protected]. > 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/CAJJc_8WQMja-zJ6VNoD%2BVWZu8TAb0Gb_yQX-NCEa2vApae_dGQ%40mail.gmail.com > . > For more options, visit https://groups.google.com/groups/opt_out. > -- 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 [email protected]. To post to this group, send email to [email protected]. 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/CAHSNPWsWr1JBUTZz6zLOh%2BmdUF%2Bb%3DDfejtbxuC16znYo%3DymHDA%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.

