开发者

get_profile alternative in for loop django

开发者 https://www.devze.com 2023-04-12 03:36 出处:网络
I\'m trying to get information of a users avatar based in an extended profile model. I usually call the information via get_profile(). However on this occasion the call is within a for loop in the tem

I'm trying to get information of a users avatar based in an extended profile model. I usually call the information via get_profile(). However on this occasion the call is within a for loop in the template and I get errors if one of the users are the same.

How would I go about avoiding this error?

{% 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开发者_StackOverflow中文版 }}
                            </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 %}      

The problem is here:

{% thumbnail fevent.owner.get_profile.avatar.get_absolute_url 120x120 crop,upscale %}

Error Message returned:

Caught MultipleObjectsReturned while rendering: get() returned more than one UserProfile -- it returned 2! Lookup parameters were {'user__id__exact': 4L}


To expand on what Matt said, while using get_or_create is a good idea, you should definitely define your profile model's User link with a OneToOneField, instead of a ForeignKey.

user = models.OneToOneField(User, verbose_name=_(u'user'))

Now, if you forget to use get_or_create(), or somehow accidentally try to create a duplicate profile for the same user, the database will raise an IntegrityError.


That error means there are two UserProfile objects in the database matching the query used by get_profile, not that get_profile was called twice. You need to remove one of those profile objects from the database and make sure there aren't multiples created again. You should be able to use the get_profile method multiple times without issue. Maybe you have a get_or_create call in that function without checking the proper values.

0

精彩评论

暂无评论...
验证码 换一张
取 消