On Sep 6, 6:27 pm, Shamail Tayyab <[email protected]> wrote:
> Hi,
>
> I've a model something like this:
>
> class Article ( models.Model ):
>
> text = models.TextField ()
> lasteditedby = models.ForeignKey ( User )
> whenedited = models.DateTimeField ( default = datetime.datetime.now() )
>
> class Topic ( models.Model ):
>
> name = models.CharField ( max_length = 1024 )
> article = models.ForeignKey ( Article )
>
> class Sections ( models.Model ):
>
> name = models.CharField ( max_length = 1024 )
> topic = models.ManyToManyField ( Topic )
>
> Now If I do, Topic.objects.all().order_by("-id")[:10] it gives me all
> the recent topics, I need to know which section this topic belong to.
> How can I do that?
>
> Thanks
>
> --
> Shamail Tayyab
> Blog:http://shamail.in/blog
Each topic has *multiple* sections - that's the whole point of a
ManyToMany relationship.
You can iterate through the sections for each topic:
for topic in topics:
for section in topic.sections.all():
print section.name
--
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.