Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 在ListView中使用两个模型_Django_Python 3.x_Django Views - Fatal编程技术网

Django 在ListView中使用两个模型

Django 在ListView中使用两个模型,django,python-3.x,django-views,Django,Python 3.x,Django Views,我的交易列表中的每个预算类别都有一个列表视图。对于这些视图中的每一个,我都想显示这个类别的实际预算。例如,我的账单预算包含租金、保险、电话等的子预算,这些子预算存储在与交易分开的模型中。当前ListView只发送按预算类型筛选的交易记录: class BillListView(ListView): model = Transaction template_name = 'budget/base_transactions.html' context_object_name

我的交易列表中的每个预算类别都有一个列表视图。对于这些视图中的每一个,我都想显示这个类别的实际预算。例如,我的账单预算包含租金、保险、电话等的子预算,这些子预算存储在与交易分开的模型中。当前ListView只发送按预算类型筛选的交易记录:

class BillListView(ListView):
    model = Transaction
    template_name = 'budget/base_transactions.html'
    context_object_name = 'transactions'
    paginate_by = 10
    queryset = Transaction.objects.filter(budget_type__exact='bill')
是否有办法从我的预算数据库发送次级预算数据,以便我可以将其显示在模板顶部

我的模型:

class Transaction(models.Model):
    date = models.DateField(default=datetime.date.today)
    description = models.CharField(max_length=100, default="")
    category = models.CharField(max_length=100, default="")
    amount = models.DecimalField(max_digits=10, decimal_places=2, default=0.0)
    budget_type = models.CharField(max_length=100, default="")

    def __str__(self):
        return self.description + ' ' + str(self.amount)

class Budget(models.Model):
    category = models.CharField(max_length=100, default="")
    sub_category = models.CharField(max_length=100, default="")
    amount = models.DecimalField(max_digits=10, decimal_places=2, default=0.0)

如果要通过
列表视图
传递另一个模型的数据,可能需要覆盖
列表视图
获取上下文数据
方法。然后,您可以将所需的数据与我们的模型对象一起传递

例如:

在您想要显示预算对象数据的上下文中,您可以将
BillListView
视图更改为:

class BillListView(ListView):
    model = Transaction
    template_name = 'budget/base_transactions.html'
    context_object_name = 'transactions'
    paginate_by = 10
    queryset = Transaction.objects.filter(budget_type__exact='bill')

    def get_context_data(self, **kwargs):
        context = super(BillListView, self).get_context_data(**kwargs)
        context['budgets'] = Budget.objects.filter(category__exact='bill') //filter as per required
        return context
然后,您可以通过循环通过
预算
访问模板中的
预算
对象,如下所示:

{% for budget in budgets %}
    {{ budget.amount }}
    {{ budget.sub_category }}
{% endfor %}

如果要通过
列表视图
传递另一个模型的数据,可能需要覆盖
列表视图
获取上下文数据
方法。然后,您可以将所需的数据与我们的模型对象一起传递

例如:

在您想要显示预算对象数据的上下文中,您可以将
BillListView
视图更改为:

class BillListView(ListView):
    model = Transaction
    template_name = 'budget/base_transactions.html'
    context_object_name = 'transactions'
    paginate_by = 10
    queryset = Transaction.objects.filter(budget_type__exact='bill')

    def get_context_data(self, **kwargs):
        context = super(BillListView, self).get_context_data(**kwargs)
        context['budgets'] = Budget.objects.filter(category__exact='bill') //filter as per required
        return context
然后,您可以通过循环通过
预算
访问模板中的
预算
对象,如下所示:

{% for budget in budgets %}
    {{ budget.amount }}
    {{ budget.sub_category }}
{% endfor %}

你也可以发布你的模型吗?你的模型
交易
预算
没有任何关系吗?它们与category属性相关。在这种情况下,我如何利用这一点?我很难理解您的模型,因为您的模型中没有定义外键或关系字段。事务模型从我的帐户中提取所有事务。预算模型存储子类别预算。我是否需要外键才能使我正在尝试的工作正常?您也可以发布您的模型吗?您的模型
交易
预算
没有任何关系吗?它们与类别属性相关。在这种情况下,我如何利用这一点?我很难理解您的模型,因为您的模型中没有定义外键或关系字段。事务模型从我的帐户中提取所有事务。预算模型存储子类别预算。我需要外键吗?太好了,这就是我需要的,谢谢。我确信我的代码结构不完美,这可能就是为什么我很难解释它的原因。但这正是我所需要的,谢谢。我确信我的代码结构不完美,这可能就是为什么我很难解释它的原因。但这就是我所需要的