B
B
bituke2021-07-26 18:07:35
Django
bituke, 2021-07-26 18:07:35

How to get a filtered list of objects in a function under a model?

I have a client model:

class Client(models.Model):
  investmen = models.ForeignKey(InvestmentAdvisor, on_delete=models.CASCADE, related_name='client')
  name = models.CharField(max_length=255)
  description = models.TextField(blank=True)
  date_of_birth = models.DateField()
  retirement_date = models.DateField()
  drawdown_behavior = models.PositiveSmallIntegerField(choices=DRAWDOWN_BEHAVIOR,)
  employment_status = models.PositiveSmallIntegerField(choices=EMPLOYMENT_STATUS,)

  def get_filter_portfolio(self):
                return self.portfolio.objects.filter(name='123213')

I'm trying to call the get_filter_portfolio function but it throws an error 'RelatedManager' object has no attribute 'objects'
How can I solve this problem? So that the function would work specifically "from under the model", so that I could display the client's portfolios in the django template engine cycle. Thank you very much in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2021-07-26
@bituke

bituke you don't need to use objects. objects returns Manager, and your portfolio is already a manager, so you get another manager from the manager.
self.portfolio.filter(name='123213')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question