每个用户都有很多 相册,每个相册都有很多 照片。每个用户都有一组 背景图像来保存它的许多图像。类似地,用户拥有一组个人资料图片来保存其许多图像。
这些是我的模型:
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)
例如:我尝试这样做:让用户将一张 图像从ProfilePicture复制到Background Image,但没有奏效:
# 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'
所以我的问题是,如何将对象从一个模型复制到另一个模型?任何建议将不胜感激。