Guest User

Untitled

a guest
Feb 16th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. def PortfolioView(request):
  2. template_name = 'portfolio/portfolio.html'
  3.  
  4. if request.method == "GET":
  5.  
  6. id_list = []
  7. queryset = PortfolioModel.objects.filter(insert_type='position')
  8. for obj in queryset:
  9. id_list.append(obj.trade_id)
  10. # No need to dive into details here, but this simply creates a list
  11. # of all available unique trade_id's in the model (there is only
  12. # one object with insert_type='position' for each 'trade_id')
  13.  
  14. portfolio_object = PortfoliopositionFormset(queryset=PortfolioModel.objects.all())
  15.  
  16. context = {
  17. 'portfolio_object': portfolio_object,
  18. 'id_list': id_list,
  19. }
  20.  
  21. return render(request, template_name, context)
  22.  
  23. elif request.method == "POST":
  24.  
  25. portfolio_object = PortfoliopositionFormset(request.POST)
  26.  
  27. if portfolio_object.is_valid():
  28.  
  29. for form in portfolio_object:
  30. form.save()
  31.  
  32. return HttpResponseRedirect('/portfolio/')
  33.  
  34. {% for id in id_list %}
  35. <form method="POST">
  36. {% csrf_token %}
  37. <table>
  38. {{ portfolio_object.management_form }}
  39. {% for object in portfolio_object %}
  40. {% if object.trade_id.value == id %}
  41. <tr>
  42. <td>{{ object.field_1 }}</td>
  43. <td>{{ object.field_2 }}</td>
  44. </tr>
  45. {% endif %}
  46. {% endfor %}
  47. </table>
  48. <button type="submit">Close position</button>
  49. </form>
  50. {% endfor %}
Add Comment
Please, Sign In to add comment