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

  • Home
  • SEARCH
  • 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 151921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:31:51+00:00 2026-05-11T09:31:51+00:00

I have a Model as follows: class TankJournal(models.Model): user = models.ForeignKey(User) tank = models.ForeignKey(TankProfile)

  • 0

I have a Model as follows:

class TankJournal(models.Model):     user = models.ForeignKey(User)     tank = models.ForeignKey(TankProfile)     ts = models.IntegerField(max_length=15)     title = models.CharField(max_length=50)     body = models.TextField() 

I also have a model form for the above model as follows:

class JournalForm(ModelForm):     tank = forms.IntegerField(widget=forms.HiddenInput())       class Meta:         model = TankJournal         exclude = ('user','ts') 

I want to know how to set the default value for that tank hidden field. Here is my function to show/save the form so far:

def addJournal(request, id=0):     if not request.user.is_authenticated():         return HttpResponseRedirect('/')      # checking if they own the tank     from django.contrib.auth.models import User     user = User.objects.get(pk=request.session['id'])      if request.method == 'POST':         form = JournalForm(request.POST)         if form.is_valid():             obj = form.save(commit=False)              # setting the user and ts             from time import time             obj.ts = int(time())             obj.user = user              obj.tank = TankProfile.objects.get(pk=form.cleaned_data['tank_id'])              # saving the test             obj.save()      else:         form = JournalForm()      try:         tank = TankProfile.objects.get(user=user, id=id)     except TankProfile.DoesNotExist:         return HttpResponseRedirect('/error/') 
  • 1 1 Answer
  • 3 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. 2026-05-11T09:31:52+00:00Added an answer on May 11, 2026 at 9:31 am

    You can use Form.initial, which is explained here.

    You have two options either populate the value when calling form constructor:

    form = JournalForm(initial={'tank': 123}) 

    or set the value in the form definition:

    tank = forms.IntegerField(widget=forms.HiddenInput(), initial=123)  
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a Django model as follows: class Person(models.Model): first_name = models.CharField(max_length=50) last_name
I have a model that is set up as follows: class Log(models.Model): name =
I have a model Foo which have a ForeignKey to the User model. Later,
I have a model as follows: class Property < ActiveRecord::Base has_and_belongs_to_many :property_values end What
1] I have the following model: class UserReportedData(db.Model): #country selected by the user, this
Let's say I have a model like this class Foo(db.Model): id = db.StringProperty() bar
A simple question: I have a Model-View-Controller setup, with Models accessing a SQL database.
I have a model being populated by my data layer and then I have
I have some model objects I'm using in my Java client application. Later these
I have a model, Thing, that has a has_many with ThingPhoto, using Paperclip to

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.