I have two models -

class Document(models.Model):
    document_id = models.AutoField(primary_key=True)
    title = models.CharField('title', max_length=200)

    class Meta:
        ordering = ['title']

    def __str__(self):
        return "%s" % (self.title)

class Collection(models.Model):
    collection_id = models.AutoField(primary_key=True)
    name = models.CharField('title', max_length=200)
    description = models.TextField('description')
    document = models.ManyToManyField(Document,)

In admin.py, I am trying to show the documents that are in a collection. I
have this:

class CollectionAdmin(admin.ModelAdmin):
    filter_horizontal = ('document',)
    list_display = ('name', 'description', 'get_documents')

    def get_documents(self, obj):
        documents =
Collection.objects.filter(collection_id=obj.collection_id).values('document')
        templist = []
        for doc in documents:
            d = Document.objects.get(document_id=doc['document']).title
            templist.append(d)
        return templist

My question is, I getting the titles of the documents in the collection
correctly? Is there some way to get a queryset to look up the Document
fields directly, or do I have to make two queries as I am doing?

Thanks!

Mark

-- 
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAEqej2NmUSH-W2_Fd9WkmPjZzj5krk2Qmw4DyaWBkj5_mj46Kw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to