Good point.
Here's the models I'm talking about as they're slightly different than
your aforementioned solution.

class Collection(models.Model):
        title = models.CharField(max_length=50, unique=True, help_text="eg:
Pokemon, Kiss, Baseball ...")
        slug = models.SlugField(max_length=50, unique=True)
        date_added = models.DateField(auto_now=True)

class CollectionItem(models.Model):
        collection = models.ForeignKey(Collection)
        number = models.IntegerField(help_text="Last Added")
        image = models.ImageField(blank=True, upload_to='temp/')
        date_added = models.DateField(auto_now=True)


So 'Collection' would be the 'trading card' set, and 'CollectionItem'
would be the individual trading card, eg: Pokemon Card #24. We may
only have images available for a few 'Pokemon' cards, eg: #3, 4, 8, 9.
So getting a default image that way?

See what I mean?


d


On Oct 10, 12:35 pm, Tim Chase <django.us...@tim.thechases.com> wrote:
> > I'm looping through an array of 'trading card' titles. For instance:
>
> >  - Pokemon
> >  - Kiss
> >  - Baseball
>
> > Instead of showing just the title of the 'trading card', I'd like to
> > display an image of each instead.
> > But not all images are available for every card in a title, for
> > instance 'Pokemon' may only have cards/images for #4, 5, 8, 9 etc.
>
> > How can I get a list of all the titles of 'trading cards' with one
> > image alongside of the first image it finds available.
>
> You don't include code that shows how your view is handing off
> the image-list, but you can define a method on your card to
> return a sample image:
>
>    class Card(Model):
>      # ... stuff ...
>      title = CharField(...)
>      def thumbnail(self):
>        try:
>          return self.image_set[0]
>        except IndexError:
>          return default_image
>    class Image(Model):
>      card = ForeignKey(Card)
>      image = ImageField(...)
>      # ...
>
> which you can then use in your view
>
>    {% for card in cards %}
>      <div>
>       <h2>{{ card.title }}</h2>
>       <img src="{{ card.thumbnail.image.url }}" />
>      </div>
>    {% endfor %}
>
> If you have large quantities of cards, you may want to use
> .select_related() so that the image_set comes back pre-populated.
>   Otherwise, you'll net a lot of extra DB hits you may not want.
>
> -tim
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to