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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:15:43+00:00 2026-05-13T20:15:43+00:00

I want to populate two foreign key fields in one of my forms. The

  • 0

I want to populate two foreign key fields in one of my forms. The relevant bit of code is as below:

if request.method == 'POST':
    form = IssuesForm(request.POST or None)
    if request.method == 'POST' and form.is_valid():
        form.save()
else:
    form = IssuesForm(initial={'vehicle': stock_number, 'addedBy': request.user, })

vehicle points to the Vehicle class. addedBy is to contain the currently logged in user.

However the drop downs aren’t initialized as I want…I still have to select the vehicle and user. From this I have two questions:

  1. What could be the problem?
  2. What is the best way to make these forms read-only?

EDIT 1
The IssueForm class looks like this so far:

class Issues(models.Model):
   vehicle = models.ForeignKey(Vehicle)
   description = models.CharField('Issue Description', max_length=30,)
   type = models.CharField(max_length=10, default='Other', choices=ISSUE_CHOICES)
   status = models.CharField(max_length=12, default='Pending', 
     choices=ISSUE_STATUS_CHOICES)
   priority = models.IntegerField(default='8', editable=False)
   addedBy = models.ForeignKey(User, related_name='added_by')
   assignedTo = models.CharField(max_length=30, default='Unassigned')
   dateTimeAdded = models.DateTimeField('Added On', default=datetime.today, 
     editable=False)
   def __unicode__(self):    
    return self.description

Form Class

class IssuesForm(ModelForm):   
  class Meta:
    model = Issues
    exclude = ('assignedTo')
  • 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-13T20:15:43+00:00Added an answer on May 13, 2026 at 8:15 pm

    For your second question, are you wanting to make the addedBy field read-only? If so, don’t add it to your form (it’ll never be read-only if you present it to the user, e.g. Firebug). You can instead populate it inside your save method.

    if request.method == 'POST':
        form = IssuesForm(request.POST or None)
        if request.method == 'POST' and form.is_valid():
            issue = form.save(commit=False)
            issue.addedBy = request.user
            # any other read only data goes here
            issue.save()
    else:
        form = IssuesForm(initial={'vehicle': stock_number}) # this is related to your first question, which I'm not sure about until seeing the form code
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my view, I want two buttons to populate three hidden fields of a
I want to populate GridView below with images: <asp:GridView ID=GrdDynamic runat=server AutoGenerateColumns=False> <Columns> </Columns>
I have two dates fields for fromdate and todate Based on that i want
Want to populate two NSMutableArray s to 2 Custom Sections of tableView ; I
I have two databases: Transactions and Customer List I want to populate the customer
I have a two column NSTableView that I want to populate with entries grabbed
I have the following code, which works fine if you just want to populate
I am having a bit following the a method should only do one thing
My code is simply intending to populate two drop down lists with the names
I am using ext js to populate my two combo boxes. I want the

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.