我試圖獲取基於擴展配置文件模型的用戶頭像的信息。我通常通過get_profile()調用信息。但是,在這種情況下,調用在模板中的for循環內,如果其中一個用戶相同,我會得到錯誤。for循環中的get_profile替代方法django
我該如何去避免這個錯誤?
{% for fevent in fevents %}
<!-- EVENT ><! -->
<div class="event">
<div class="pic">
<a href="" class="notification" title="{{ fevent.getPublishedPredictionsCount }} Predictions">{{ fevent.getPublishedPredictionsCount }}</a>
<img src="{% thumbnail fevent.artwork.get_absolute_url 100x98 crop,upscale %}" alt="" width="100" height="98" />
<div class="overlay">
<a href=""></a>
</div>
</div>
<h1><a href="">{{ fevent.title|trunchar:30 }}</a></h1>
{% autoescape off %}
{{ fevent.getEventPredictionScore|makestars }}
{% endautoescape %}
<ul class="details">
<li class="cat">
Category: <a href="">{{ fevent.catagory }}</a>
</li>
<li class="location">
{{ fevent.location }}
</li>
<li class="date">
{{ fevent.date_and_time }}
</li>
<li class="time">
7:00pm - 8:00pm
</li>
</ul>
<!-- CLEAR ><! --><div class="clear"></div>
<div class="hype">
<div class="avatar">
<a href="" class="overlay" title="{{ fevent.owner.get_full_name }}"></a><img src="{% thumbnail fevent.owner.get_profile.avatar.get_absolute_url 120x120 crop,upscale %}" alt="" width="120" height="120" />
</div>
<p>{{ fevent.description|trunchar:200 }}… <a href="">Read More</a></p>
</div>
<!-- CLEAR ><! --><div class="clear"></div>
</div>
<!-- END OF EVENT ><! -->
{% endfor %}
的問題是在這裏:
{% thumbnail fevent.owner.get_profile.avatar.get_absolute_url 120x120 crop,upscale %}
錯誤消息返回:
Caught MultipleObjectsReturned while rendering: get() returned more than one UserProfile -- it returned 2! Lookup parameters were {'user__id__exact': 4L}
請添加錯誤信息。 –
編輯,對此感到遺憾。 –
這是[在你的個人資料中有'用戶'FK的缺點](http://stackoverflow.com/questions/3221745/django-when-extending-user-better-to-use-onetoonefielduser-or-foreignkeyuser )。 –