Jorge - that is exactly what I was after - thanks very much! I don't have too many categories, and don't expect there to be more than 100 or so - so the convenience outweighs the overhead.
This way, I'm even able to add filters before doing the iteration. -Phil On 12/07/06, Jorge Gajon <[EMAIL PROTECTED]> wrote: > > Hi, > > If there are not a lot of categories (i.e. a thousand or more), you > could iterate over all categories and filter only those that have a > photo_set of 1 or more. For example in a view you could do: > > categories = [c for c in Category.objects.all() if c.photo_set.count()] > > You could say that it is not very optimal because you are fetching all > the categories. But I personally wouldn't worry about it if there are > not many categories. > > Cheers, > Jorge > > On 7/12/06, Phil Powell <[EMAIL PROTECTED]> wrote: > > > > Hi - I've got a bit of a conundrum. > > > > I have two models, Category and Photo which look something like this: > > > > class Category(models.Model): > > title = models.CharField(maxlength=80) > > slug = models.SlugField(prepopulate_from=("title",)) > > > > class Photo(models.Model): > > catalogue_number = models.IntegerField(maxlength=80) > > slug = models.SlugField(prepopulate_from=("catalogue_number",)) > > category = models.ForeignKey(Category,blank=True) > > > > There is a one-to-many relationship defined so that a Photo can be > > assigned a Category. > > > > Now, what I want to be able to do is get a list of Categories, but > > ONLY those Categories which have been assigned to one or more Photos. > > > > I can think of plenty of ugly ways to work around this, but wondered > > whether anyone had a graceful solution? > > > > -Phil > > > > > > > > > > > --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~----------~----~----~----~------~----~------~--~---