Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 834593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:41:45+00:00 2026-05-15T04:41:45+00:00

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

  • 0

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):
        DiscussionTemplate = 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T04:41:45+00:00Added an answer on May 15, 2026 at 4:41 am

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 443k
  • Answers 443k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Just document it clearly that no other initialiser but that… May 15, 2026 at 6:09 pm
  • Editorial Team
    Editorial Team added an answer It's easy to test - the second method gets called.… May 15, 2026 at 6:09 pm
  • Editorial Team
    Editorial Team added an answer Note that NSTimeInterval uses seconds, not milliseconds. Here is some… May 15, 2026 at 6:09 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.