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'.
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.
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.
Here's my code:
Models.py:
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
forms.py:
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"]
views.py:
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)
live_bid_truck.html
{% for post in post_queryset %}
<table class="table table-striped, sortable">
<thead>
<tr>
<th>Load No.</th>
<th>From</th>
</tr>
</thead>
<tbody>
<tr> // Returns the post_queryset with radio buttons
<td >{{form.post}} </td>
<td>{{ post.from1 }}</a><br/></td>
</tr>
</tbody>
</table>
<table class="table table-striped, sortable " style="margin-top: 10px">
<thead>
<tr>
<th> Amount</th>
</tr>
</thead>
<tbody>
<tr>
<form class="nomargin" method='POST' action='' enctype='multipart/form-data'>{% csrf_token %}
<td>{% render_field form.amount class="form-control" %}</td>
</tr>
</tbody>
</table>
<input type='submit' value='Post Bid'/></form>
{% endfor %}
Update - 1:
Views.py:
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)
live_bids.html:
{% for post in post_queryset %}
{{post.id}}
{% for bid in post.bids.all %}
{{bid.amount}}
{% endfor %}
{% endfor %}