On Feb 9, 4:51 am, Martin <[EMAIL PROTECTED]> wrote:
> Hi Rajesh,
>
> thanks for your quick reply. I don't really understand your point but
> i'll try to explain mine:
> get_article_list is a queryset, it filters live articles and compares
> them to todays date and returns only the ones that are in future. But
> the thing is, before manytomany i had video field as foreignkey to
> Video model and stuff worked - i used my templatetag and add
> attributes (in for loop) .video, .body, .pub_date.
> I really don't understand why this doesn't work - i thought i could
> just loop through videos of an article and just display the first one
> (it's like a thumbnail for an article).
There is a difference between having video as a ForeignKey and having
it as a M2M field. So, the code that worked when it was an FK is not
going to work when it's an M2M. So, article.video.all() is now a
Queryset. You are trying to loop over article_list.video.all but
article_list is a Queryset that does not have a video attribute so
that statement is invalid.
Perhaps this will help you see my point better:
{% get_article_list as article_list %}
{% for article in article_list %}
{% for video in article.video.all %}
{% if forloop.first %}
<object>
<code for displaying youtube videos>
{{ video.name }}, {{ video.url }}
</object>
{% endif %}
{% endfor %}
{% endfor %}
This is not the most efficient way to solve the problem, though.
That's because the inner loop over each video of an article could
potential loop over a hundred videos and only display one of them.
I am not sure if list-indexing works on Querysets like
article.video.all(). So, you might want to try the following which is
closer to what you were originally trying to do (but, again, I am not
sure if this works):
{% get_article_list as article_list %}
{% for article in article_list %}
<object>
<code for displaying youtube videos>
{{ article.video.all.0.name }}, {{ article.video.all.0.url }}
</object>
{% endfor %}
If this doesn't work but you would like something similar to it, you
could add a method called firstvideo() to your Article model like
this:
def firstvideo(self):
return self.video.all()[0]
Then, the template would go like this:
{% get_article_list as article_list %}
{% for article in article_list %}
<object>
<code for displaying youtube videos>
{{ article.firstvideo.name }}, {{ article.firstvideo.url }}
</object>
{% endfor %}
I would recommend taking another look at my original solution of
creating a new templatetag that's capable of displaying the first
video of an article. That solution is less complicated than it
looks :) It let's you abstract your YouTube HTML excerpt nicely into a
separate template file making it more reusable. And it also keeps your
main templates cleaner.
One last thing: it's a convention to use plural words for M2M fields.
So, I would rename Article.video to Article.videos. It makes the
meaning of that field clearer in templating and view code.
Good luck!
-Rajesh
--~--~---------~--~----~------------~-------~--~----~
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?hl=en
-~----------~----~----~----~------~----~------~--~---