开发者

get request.session from a model method in django

开发者 https://www.devze.com 2023-01-02 12:47 出处:网络
Hay, is it possible to a get a request.session value from a model method in django? MEGA UPDATE Here is my model

Hay, is it possible to a get a request.session value from a model method in django?

MEGA UPDATE

Here is my model

class GameDiscussion(models.Model):
    game = models.ForeignKey(Game)
    message = models.TextField()
    reply_to = models.ForeignKey('self', related_name='replies', null=True, blank=True)
    created_on = models.DateTimeField(blank=True, auto_now_add=True)
    userUpVotes = models.ManyToManyField(User, blank=True, related_name='threadUpVotes')
    userDownVotes = models.ManyToManyField(User, blank=True, related_name='threadDownVotes')
    votes = models.IntegerField()

    def html(self):
        DiscussionTe开发者_JAVA百科mplate = loader.get_template("inclusions/discussionTemplate")
        return DiscussionTemplate.render(Context({
            'discussion': self,
            'replies': [reply.html() for reply in self.replies.all().order_by('-votes')]
    }))

    def _find_users_who_have_voted(self):
        user_list = []
        for user in self.userDownVotes.all():
            user_list.append(user.id)
        for user in self.userUpVotes.all():
            user_list.append(user.id)   
        return user_list

    users_voted = property(_find_users_who_have_voted)

and my view is called like this

<ul>
    {% for discussion in discussions %}
        {{ discussion.html }}
    {% endfor %}
</ul>

and the template

<li>
<small>
    ({{ discussion.votes }} votes) 

    {% if user_id not in discussion.users_voted %}

      user not in list!

    {% endif %}     

</small>
<strong>{{ discussion.message }}</strong> 
{% if replies %}
    <ul>
        {% for reply in replies %}
            {{ reply }}
        {% endfor %}
    </ul>
{% endif %}

the value 'user_voted' returns a list of user ids who has voted on this discussion.

I want to see if the request.session['user'].id value is inside this list


Why don't you use Django's render_to_string inside a view, which would have request happily available to it, and avoid model method altogether?

UPDATE: after skimming your mega update, you should look into Django's inclusion tags and use the data from the model to fill a template, not use a model to render the template. Keep your model and your template separate - Django is an MVT / MCV framework for good reason :)


you can access the current user, and so their session using threadlocals middleware

http://code.arcs.org.au/gitorious/django/django-andsome/blobs/ee8447e3dad2da9383ff701ec640b44cd50d2b0a/middleware/threadlocals.py

but keep in mind:

http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser

There might be better solutions to your problem. Maybe you'd like to elaborate why you need request.session on model level?

UPDATE:

since you explicitly call some method - probably from a view - why don't you just put the request.user as parameter to your html method?

models.py:

def html(self, user):
    your code...

views.py:

yourmodel.html(request.user)

UPDATE to your MEGA UPDATE:

This is exactly what {% include %} is for:

in your first template do this:

{% for discussion in discussions %}
    {% include "discussion.html" }}
{% endfor %}

and the second template has request.user.id in its namespace:

{% if request.session.user.id not in discussion.users_voted %}
  user not in list!
{% endif %}     
0

精彩评论

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

关注公众号