B
B
bituke2021-06-20 05:51:53
Django
bituke, 2021-06-20 05:51:53

How to get filtered customer data in django?

portfolio model

class Portfolio(models.Model):
  client = models.ForeignKey(Client, on_delete=models.CASCADE, related_name='portfolio')
  name = models.CharField('название', max_length=255)
  initial_investment_amount = models.PositiveIntegerField('начальная сумма инвестиций',)
  investment_horizon = models.PositiveIntegerField('инвестиционный горизонт',)
  type_investor = models.PositiveSmallIntegerField('тип инвестора', choices=TYPE_INVESTOR)
  currency = models.PositiveSmallIntegerField('валюта', choices=CURRENCY)
  maximum_allowable_drawdown = models.PositiveIntegerField('максимально допустимая просадка',)
  type_according_risk_reward = models.PositiveSmallIntegerField('тип по соотношению риска/прибыли', choices=TYPE_RISK)
  focus = models.PositiveSmallIntegerField('фокус', choices=FOCUS)
  types_assets = models.PositiveSmallIntegerField('тип акций', choices=TYPE_ASSETS)
  ETF = models.PositiveSmallIntegerField(choices=ETF)
  active = models.BooleanField()


formation of a list of clients
all_clients = Client.objects.filter(investmen=investmen)


The task is to get all portfolios in the template with the active=True status and the same with active=False.
the template has a loop:
{% for i in all_clients %}
    {{ i.portfolio.count }}
{% endfor %}

And it displays all portfolios, but it is necessary that it displays only portfolios with the active=True status.
Thank you very much if you help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AlexNest, 2021-06-20
@AlexNest

With filters

@register.filter
def get_active_portfolio_items(client):
    return Portfolio.objects.filter(client=client, active=True)

In the template, walk like a normal querise.
{% for port in  i|get_active_portfolio_items%} 
    {{ port}}
{% endfor %}

S
Sergey Gornostaev, 2021-06-20
@sergey-gornostaev

You can send already filtered data to the template context -all_clients.filter(active=True)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question