How to copy and object from one model to another model
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
# Get its set of Background images
>>>m_back_set = m.backgroundimage_set.all()
>>>m_back_set
[<BackgroundImage: bg_mik>]
[<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
>>>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>
# 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 django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CAHSNPWue7HtdGuJeqtEPsa%2Bncc2rrpE_tEuZSXRA4NXZtsmxkA%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home