title: Python : How to pass the value of a foreign key in my Django template?
body: <p>I have created a Bid model so that type-2 users can bid on some 'Post' created by type-1 users. For achieving this, I made a foreign key for post field in the model 'Bid'.</p>
  
  <p>Actually i wanted to relate the bids to a particular post with auto generated 'id' in the 'post' model. So i placed the get_absolute_url beside Post 'id' in my template. I am new to django and I am not sure whether it works for what i want or not. </p>
  
  <p>How can i relate the bid with post_id to a particular post in the template so that i can get a bid amount placed by various type-2 users for a particular post? I would appreciate helping me solve this.</p>
  
  <p>Here's my code:</p>
  
  <p>Models.py:</p>
  
  <pre><code>class Post(models.Model):
  
      post_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
      user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
      from1 = models.CharField(max_length=20)
      timestamp = models.DateTimeField(auto_now=False, auto_now_add=True)
  
      objects = PostManager()
  
      def __str__(self):
          return self.post_id
  
      def get_absolute_url(self):
          return reverse("posts:detail", kwargs={"id": self.post_id})
  
      class Meta:
          ordering = ["-timestamp", "-Time"]
  
  class Bid(models.Model):
  
      bid_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
      post = models.ForeignKey(Post, default=uuid.uuid4, related_name='bids' )
      user = models.OneToOneField(settings.AUTH_USER_MODEL, null=True, blank=True, unique=False)
      amount = models.IntegerField()
  
      def get_absolute_url(self):
          return reverse("accept_bid", kwargs={"bid_id": self.bid_id})
  
      def __unicode__(self):
          return self.amount
  
      def __str__(self):
          return self.amount
  </code></pre>
  
  <p>forms.py:</p>
  
  <pre><code>class BidForm(forms.ModelForm):
      // Defined a post field as ModelChoiceField
      post = forms.ModelChoiceField(queryset= Post.objects.all(), label="Post", widget=forms.RadioSelect(), initial=0)
      amount = forms.IntegerField(help_text='Place the bid for a Post')
  
      def __init__(self, *args, **kwargs):
          self.request = kwargs.pop('request', None)
          super(BidForm, self).__init__(*args, **kwargs)
          self.fields['post'].queryset = Post.objects.all
  
      class Meta:
          model = Bid
          fields = ["amount"]
  </code></pre>
  
  <p>views.py:</p>
  
  <pre><code>def live_bid_truck(request, **kwargs):
      if request.method=='post':
          form = BidForm(request.POST or None)
  
          if  form.is_valid():
           bid = form.save(post)
           bid = form.save(commit=False)
           print(form.cleaned_data.get('amount'))
           bid.user = request.user
           bid.post = form.cleaned_data['post'] // Set my post here
  
  
           bid.save()
      else:
          form=BidForm()
          post_queryset = Post.objects.all()
          context = {
          "post_queryset": post_queryset, 
          "title": "List",
          'form': form,
  
      }
          return render(request, 'loggedin_truck/live_bid_truck.html', context)
  </code></pre>
  
  <p>live_bid_truck.html</p>
  
  <pre><code>{% for post in post_queryset %}
  &lt;table class="table table-striped, sortable"&gt;
      &lt;thead&gt;
          &lt;tr&gt;
  
              &lt;th&gt;Load No.&lt;/th&gt;
              &lt;th&gt;From&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt; // Returns the post_queryset with radio buttons
              &lt;td &gt;{{form.post}} &lt;/td&gt;
              &lt;td&gt;{{ post.from1 }}&lt;/a&gt;&lt;br/&gt;&lt;/td&gt;
          &lt;/tr&gt; 
      &lt;/tbody&gt;
  &lt;/table&gt;
  &lt;table class="table table-striped, sortable "  style="margin-top: 10px"&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt; Amount&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr&gt;     
              &lt;form class="nomargin" method='POST' action='' enctype='multipart/form-data'&gt;{% csrf_token %}
              &lt;td&gt;{% render_field  form.amount  class="form-control" %}&lt;/td&gt;
          &lt;/tr&gt;
       &lt;/tbody&gt;
   &lt;/table&gt;
  
  &lt;input type='submit' value='Post Bid'/&gt;&lt;/form&gt;
  
  {% endfor %}
  </code></pre>
  
  <p>Update - 1:</p>
  
  <p>Views.py:</p>
  
  <pre><code>def live_bids(request):
      post_queryset = Post.objects.all().prefetch_related('bids')
      bid_queryset = Bid.objects.all().order_by('amount')
  
      context = {
          "post_queryset": post_queryset,
          "bid_queryset": bid_queryset,
          "title": "List",
      }
      return render(request, 'loggedin_load/live_bids.html', context)
  </code></pre>
  
  <p>live_bids.html:</p>
  
  <pre><code>{% for post in post_queryset %}
  {{post.id}}
  {% for bid in post.bids.all %}
  {{bid.amount}}
  {% endfor %}
  {% endfor %}
  </code></pre>