How to add objects from one class to another

91 views
Skip to first unread message

Aamu Padi

unread,
Nov 4, 2013, 5:33:12 PM11/4/13
to django...@googlegroups.com
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.

Aamu Padi

unread,
Nov 4, 2013, 5:51:24 PM11/4/13
to django...@googlegroups.com
What I want is that, each user will have a set of `profile pictures`, `background images` and other set of `photos uploaded`. These images will be kept separately in the templates. And if the user wants, he can easily use (copy) the other set of photos uploaded or images from the background image set, as the profile picture, and that image will be added to `profile picture set`. Or if he wants, he can use an image from `profile picture set` or from the other `uploaded photos` as the `background image` and similarly that image used from other class will be copied to `background image set`.

Gonzalo Delgado

unread,
Nov 4, 2013, 7:48:15 PM11/4/13
to django...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

El 04/11/13 19:33, Aamu Padi escribi�:
I don't think Django supports that kind of queries. You can try
django-polymorphic for something very close to what you want:
https://github.com/chrisglass/django_polymorphic

My recommendation, though, would be to not use inheritance that way
and simply add an "image_type" field to your Image model and filter
that instead:

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)
image_type = models.CharField(
max_length=20, choices=(('background', 'Background'),
('profile', 'Profile')))

and do:
m.image_set.filter(image_type='background')
or:
m.image_set.filter(image_type='profile')


Another possibility would be to make Image an abstract model. Your
references to profilepic_set and backgroundpic_set will work, but you
won't be able to use the Image model to store data in the database.


- --
Gonzalo Delgado
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.15 (Darwin)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iF4EAREIAAYFAlJ4QE8ACgkQzbfdFL5JoUlCDAEAkyrstji355TouLYZDjXmax8x
mFnN0nQ2lDbo0lUNiBsA/27hAQNhX74p1Wb3O1e3qjkK23nj6NCL09GL167Knl0h
=5oiE
-----END PGP SIGNATURE-----

Aamu Padi

unread,
Nov 5, 2013, 11:14:33 AM11/5/13
to django...@googlegroups.com
Hello, Gonzalo! :D
I think abstract models is what I am after. But, I also want to get the latest photo by that user, be that a profile pic or background pic. Can I use the abstract base model, ie the Image model to atleast get the latest photo by [-pub_date], even though I wont save anything in it???


On Tue, Nov 5, 2013 at 6:18 AM, Gonzalo Delgado <ma...@gonzalodelgado.com.ar> wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

El 04/11/13 19:33, Aamu Padi escribió:

--
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/5278404F.8080805%40gonzalodelgado.com.ar.
For more options, visit https://groups.google.com/groups/opt_out.

Gonzalo Delgado

unread,
Nov 5, 2013, 12:10:48 PM11/5/13
to django...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

El 05/11/13 13:14, Aamu Padi escribi�:
> I think abstract models is what I am after. But, I also want to get
> the latest photo by that user, be that a profile pic or background
> pic. Can I use the abstract base model, ie the Image model to
> atleast get the latest photo by [-pub_date], even though I wont
> save anything in it???

Hello Aamu :-)

Sadly, you won't be able to retrieve data in any way using the
abstract model class (see
https://docs.djangoproject.com/en/1.5/topics/db/models/#abstract-base-classes
), but django-polymorphic can definitely do that.

- --
Gonzalo Delgado
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.15 (Darwin)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iF4EAREIAAYFAlJ5JpgACgkQzbfdFL5JoUnlKAD+LljmaN7XpFTdQtcZx4i9e43c
7hAZ8Y2XOv0tH5TljKcA+wQdh5fNUmk71laONL1bWBWizncxe2PGFO/+QsLyOP+e
=AI7P
-----END PGP SIGNATURE-----

Aamu Padi

unread,
Nov 5, 2013, 1:47:54 PM11/5/13
to django...@googlegroups.com
Ok, I guess, I will try that. Thank you!!!


On Tue, Nov 5, 2013 at 10:40 PM, Gonzalo Delgado <ma...@gonzalodelgado.com.ar> wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

El 05/11/13 13:14, Aamu Padi escribió:
-----END PGP SIGNATURE-----

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