-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256 El 04/11/13 19:33, Aamu Padi escribió: > 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?
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----- -- 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+unsubscr...@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/5278404F.8080805%40gonzalodelgado.com.ar. For more options, visit https://groups.google.com/groups/opt_out.