Monday, November 4, 2013

How to add objects from one class to another

This is my models.py:

    class Image(models.Model):
        user = models.ForeignKey(User)
        caption = models.CharField(max_length=300)
        image = models.ImageField(upload_to=get_upload_file_name)
        pub_date = models.DateTimeField(default=datetime.now)

        class Meta:
            ordering = ['-pub_date']
            verbose_name_plural = ('Images')

        def __unicode__(self):
            return "%s - %s" % (self.caption, self.user.username)


    class ProfilePic(Image):
        pass



    class BackgroundPic(Image):
        pass


    class Album(models.Model):
        name = models.CharField(max_length=150)

        def __unicode__(self):
            return self.name


    class Photo(Image):
        album = models.ForeignKey(Album, default=3)

And this is another:

    class UserProfile(models.Model):
        user = models.OneToOneField(User)
        permanent_address = models.TextField()
        temporary_address = models.TextField()
        profile_pic = models.ForeignKey(ProfilePic)
        background_pic = models.ForeignKey(BackgroundPic)

        def __unicode__(self):
            return self.user.username

I can access the Parent class with its User object.

    >>>m = User.objects.get(username='mika')
    >>>m.image_set.all()
    >>>[<Image: mika_photo - mika>, <Image: mika_pro - mika>, <Image: mika_bf - mika>]


But I can't access its child class with the User. I tried:

    >>>m.Image.profilepic_set.all()
and

    >>>m.image_set.profilpic.all()

and this

    >>>m.profilepic_set.all()
    AttributeError:'User' object has no attribute 'profilepic_set'

But all gave me errors!  How do I access the child classes, so that I can add images from one class to another. Like copy one image from Photo to ProfilePic, or from ProfilePic to BackgroundPic and so. Or simply, how to add images for particular User in specific classes?

Please guide me to achieve the above mentioned. 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/CAHSNPWsBg1sgWTQF%3D5zzKXiz2-yt8twVDnv8xP1bM2tJeZF4qw%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


Real Estate