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 1033811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:17:21+00:00 2026-05-16T14:17:21+00:00

I my model users can create rifles and this rifle is obviously associated with

  • 0

I my model users can create rifles and this rifle is obviously associated with a User.

class Gun(ImageModel):
    user = models.ForeignKey(User)  
    ...  
    ...  
    ...

I have another model which is dependent on this and need to make use of the users rifles, but when the user adds a record I only want to display his rifles.

mt model looks as follows

class Trophies(ImageModel):  
    used_his = models.ForeignKey(Gun)

my form looks as follows

from django.forms import ModelForm
from django import forms
from models import Trophies
from gunsafe.models import Gun
from django.contrib.auth.models import User

class TrophiesForm(request.user, ModelForm):
    used_his = forms.ModelMultipleChoiceField(queryset=Gun.objects.filter(user__id=1))

    def __init__(self, user, *args, **kwargs):
        super(TrophiesForm, self).__init__(*args, **kwargs)
        self.fields['used_his'].queryset = User.objects.filter(pk = user)

I was wondering how I can get the current logged in users ID instead of the user__id=1

Here is the view.

def edit(request, trophy_id, template_name='trophies/edit.html'):
    trophy = Trophies.objects.get(pk=trophy_id)

    if request.method == 'POST':
        form = TrophiesForm(request.POST, request.FILES, instance=trophy)
        if form.is_valid():
            newform = form.save(commit=False)
            newform.user = request.user
            newform.save()  
            ...  
            ...
  • 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-16T14:17:22+00:00Added an answer on May 16, 2026 at 2:17 pm

    I think you can achieve this by overriding the __init__() method of the form, passing in an instance of User and filtering the queryset using that user. Something like this:

    class TrophiesForm(ModelForm):
        used_his = forms.ModelMultipleChoiceField(queryset=Gun.objects.filter(user__id=1))
    
        def __init__(self, user, *args, **kwargs):
            super(TrophiesForm, self).__init__(*args, **kwargs)
            self.fields['used_his'].queryset = User.objects.filter(pk = user.id)
    

    In your view you can pass in the appropriate (currently logged in) instance of User.

    def my_trophies(request, *args, **kwargs):
        user = request.user
        form = TrophiesForm(user)
        ... 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please help me model this in SQLAlchemy. A user can create a question. A
I would like to create model binding functionality so a user can enter ','
Can any one volunteer why the class below fails? ... src/model/user.rb:18: undefined method `set_schema'
My application has three devise models: user, employer and auditor. I can create all
I have this rails model association (has_many :through) class User < ActiveRecord::Base has_many :assignments
I have started this support ticket project where users can create tickets and the
I am trying to build a relationship model between users. A user can either
I am trying to create a school model, which users can create, with the
I'm trying to set up a simple Roles model for my site. Users can
I have two model: User, Article A user can like or dislike many articles,

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.