views:

35

answers:

1

I need some help (sorry for my poor english).

I 'm trying to get all photos from each album. I don't know how to make the query. I need this data (and order the photos by atribute "order" it will fantastic).

House1-title

photo1: descritpion

photo2: descritpion

photo3: descritpion

House2-title

photo1: descritpion

photo2: descritpion

photo3: descritpion

The model (abbreviated) looks like this, which is more easy to make the query? and wich is more efficient? (i know, the best solution is using memcache).

Option A:

class House(ImageModel):
      title = models.CharField(max_length=25)
      photos = models.ManyToManyField('Photo')


class Photo(ImageModel):
    photo = models.ImageField(upload_to='photos/originals')
    description = models.CharField(max_length=100)
    order = models.IntegerField()

Option B:

class House(ImageModel):
      title = models.CharField(max_length=25)

class Photo(ImageModel):
    house = models.ForeignKey(House)
    photo = models.ImageField(upload_to='photos/originals')
    description = models.CharField(max_length=100)
    order = models.IntegerField()

Thanks !

+2  A: 

option B. is the right way.

House.objects.all().select_related()


{% for house in house_qs %}
     {{house.name}}

    {% for photo in house.photo_set.all %}
       {{photo}}
    {% endfor %}

{% endfor %}
Lakshman Prasad
It's possible to order with order_by? this crash my app{% for photo in photographer.photo_set.all.order_by('order') %}
user: in the photo model, define a meta class in which you define it's ordering by the field `order`
Lakshman Prasad