M
M
maxclax2015-12-05 14:46:30
Django
maxclax, 2015-12-05 14:46:30

How to filter a selection in a template?

I have:

class Data(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('User'))
    number = models.CharField(_('Number'), unique=True, max_length=6)

class Wallet(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('User'))
    data = models.ForeignKey(m.Data, null=True, verbose_name=_('Data'))

class DataListView(LoginRequiredMixin, TemplateView, BaseContextMixin):
    template_name = "data/list.html"
    login_url = reverse_lazy('users:login')

    def get_context_data(self, **kwargs):
        context = super(DataListView, self).get_context_data(**kwargs)
        context['my_data'] = self.request.user.data_set.all()
        return context

then I output in the template:
{% for one in my_data %}

{% endfor %}

And I get the data of the current user: context['my_data'] = self.request.user.data_set.all()
Everything is fine, but inside the for loop I need to display all wallets of this user for this date. I use
{% for wallet in one.wallet_set.all %}

{% endfor %}

As a result, I get all wallets that do not even belong to the current user. How to set up a filter so that wallets are only from the current user? How to use filter by models in templates?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2015-12-05
@maxclax

How to filter a selection in a template?
no way.
Disgusting practice
Filter in the model, as a last resort, in the view

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question