Djang上的属性错误

2024-09-27 00:13:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我被Django的一个错误缠住了(我想),我找不到错误的地方。你知道吗

关于“目录”/型号.py“我有(它连接到MySQL数据库):

from django.db import models

class Application(models.Model):
    nameApp = models.CharField(max_length=50)
    tarification = models.ForeignKey(Tarification)

然后,我使用django-tables2(Doc to fill tables)在django上生成表,在我的表格.py我有:

import django_tables2 as tables
from catalog.models import AppCost, Application, Tarification


class BillTable(tables.Table):
    class Meta:
        appName = Application.nameApp
        userApp = AppCost.userApp
        tarifName = Tarification.nameTarif
        tarifCost = Tarification.cost
        startTime = AppCost.startTime
        finishTime = AppCost.finishTime
        totalCost = AppCost.totalCost
        # add class="paleblue" to <table> tag
        attrs = {'class': 'paleblue'}

当我呈现我的网站时出现了一个错误:

type object 'Application' has no attribute 'nameApp'

BillTableappName = Application.nameApp行上

但是,查看Pycharm上的“数据库”窗口,我看到了表模式,它是:

  • 目录应用程序
    • 身份证
    • 焦油化
    • 名称应用程序
    • 其他东西

使用MySQL Workbench,模式看起来是一样的。那么,为什么我会犯这个错误呢?你知道吗

敬礼。你知道吗


Tags: djangofrompyimport目录数据库tablesapplication
2条回答

正如Daniel Roseman上面提到的,您可能要查找的代码如下,它不需要新的模型:

import django_tables2 as tables
from catalog.models import AppCost, Application, Tarification

class AppCostTable(tables.Table):
    userApp = tables.Column()
    startTime = tables.Column()
    finishTime = tables.Column()
    totalCost = tables.Column()

    class Meta:
        model = AppCost

class ApplicationTable(tables.Table):
    appName = tables.Column(accessor='nameApp')
    class Meta:
        model = Application

class TarificationTable(tables.Table):
    tarifName = tables.Column(accessor='nameTarif')
    tarifCost = tables.Column(accessor='cost')
    class Meta:
        model = Tarification


class BillTable(AppCostTable, ApplicationTable, TarificationTable, tables.Table):
    pass

如果您不介意换一个型号,那么在目录.模型您可以添加新的帐单模型:

class Bill(models.Model):
    application = models.ForeignKey('Application')
    appcost = models.ForeignKey('AppCost')
    tarification = models.ForeignKey('Tarification')

在表文件中:

from catalog.models import Bill

class BillTable(tables.Table):
    appName = tables.Column(accessor='application.nameApp')
    tarifName = tables.Column(accessor='tarification.nameTarif')
    tarifCost = tables.Column(accessor='tarification.cost')
    userApp = tables.Column(accessor='appcost.userApp')
    startTime = tables.Column(accessor='appcost.startTime')
    finishTime = tables.Column(accessor='appcost.finishTime')
    totalCost = tables.Column(accessor='appcost.totalCost')


    class Meta:
        model = Bill

你对如何使用django表很困惑。您需要在元类中指定一个模型,然后只需使用fields属性添加该模型中的字段列表(作为字符串)即可显示。不能只指定三个任意模型中的字段。你知道吗

相关问题 更多 >

    热门问题