2016-09-01 8 views
0

私はDjangoのダミーエラーがどこにあるのかわかりません。 "カタログ/ models.py" オンDjangoのAttributeError

私は(これは、MySQLデータベースに接続します)があります。次に

from django.db import models 

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

、私は、Djangoの上のテーブルを作るためにジャンゴ - tables2Doc to fill tables)を使用していますので、私のtables.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'} 

そして、私はエラーを取得する:

01ライン appName = Application.nameApp

BillTableから

しかし、私はテーブルのスキーマを参照し、それはだPycharmの "データベース" ウィンドウを見て:

  • catalog_application
    • ID
    • tarification_id
    • nameApp
    • 他のもの

とMySQL Workbenchでスキーマを見ると同じように見えます。だから、なぜこのエラーが出ていますか?

よろしくお願いいたします。

+0

PEP-8に従うことを検討し、[命名規則](https://www.python.org/dev/peps/pep-0008/#id36) –

答えて

1

ダニエルローズマンは、前述したように、あなたが探している可能性のあるコードは以下の通りです、それは新しいモデルは必要ありません:あなたは、あなたのカタログ内で、別のモデルを持っている気にしない場合は

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 
+0

こんにちは、私は知っているが、私はデータベース内の複数のテーブルから異なるフィールドを持つテーブルを作る方法を探しているか、 "django_tables2"を実行できませんか?あなたは私がそれをすることで何かを知っていますか? – Aker666

+0

こんにちは、@ Aker666。コメントの最後にリンクを見たいかもしれません。単一のビューで複数のテーブルを使用する方法について説明します。しかし、この文書にはあまり詳しくは書かれていません。あなたに質問がある場合はお知らせください。 https:// django-tables2。readthedocs.io/en/latest/pages/generic-mixins.html#multiple-tables-using-multipleobjectmixin –

+0

それは動作し、それは私が探していたものです。私は2番目の方法を使用しました(ビルドモデルをカタログ内に追加します)。私がそれを理解するのを助けてくれてありがと – Aker666

1

あなたはdjango-tableの使い方が非常に混乱しています。 Metaクラスには1つのモデルを指定し、fields属性のみを指定して、そのモデルのフィールドのリストを文字列として表示する必要があります。任意の3つのモデルからフィールドを指定することはできません。

+0

を見てみると、別の方法でテーブルを作ることができますデータベース内の複数のテーブルのフィールド? – Aker666

関連する問題