On Oct 21, 12:47 pm, Franklin Einspruch <[email protected]>
wrote:
> In my database there are tables for ArtworkImage, Artwork, and Artist.
> Every ArtworkImage belongs to an Artwork, and every Artwork belongs to
> an Artist.
>
> class Artist(models.Model):
> directory = models.CharField(max_length=128) # this is a lowercase
> version of the artist's name
> ...
>
> class Artwork(models.Model):
> artist = models.ForeignKey(Artist)
> ...
>
> class ArtworkImage(models.Model):
> artwork = models.ForeignKey(Artwork)
>
> I spent yesterday trying unsuccessfully to write a query that would
> return a set of ArtworkImages associated with a particular
> Artist.directory. The docs about reverse foreign-key lookups seem to
> indicate that this is possible:
>
> artist = Artist.objects.get(directory='smith')
> artst_image = artist.artworkimage_set.all()[0] # I just want one
>
> I've clearly misunderstood something because this throws
>
> > 'Artist' object has no attribute 'artworkimage_set'
>
> Thanks for the help.
>
> Franklin
As you state, the foreign key is from artworkimage to artwork, not to
artist. So artwork will have an artworkimage_set property, but artist
won't.
The way to do this is to turn the query around, and start from
artworkimage. Then you can use the double-underscore syntax to
traverse the relations to find all images related to an artist:
ArtworkImage.objects.filter(artwork__artist=my_artist)
--
DR.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.