Reputation: 540
I'm stuck with (I think) a dummy error on Django that I can't find where it's the fault.
On "catalog/models.py" I have (it connects to a MySQL database):
from django.db import models
class Application(models.Model):
nameApp = models.CharField(max_length=50)
tarification = models.ForeignKey(Tarification)
Then, I'm using django-tables2 (Doc to fill tables) to make tables on Django, so on my tables.py I have:
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'}
And I get an error when I render my website:
type object 'Application' has no attribute 'nameApp'
On the line appName = Application.nameApp
from BillTable
But, looking at "Database" window on Pycharm I see the table schema and it's:
And looking with MySQL Workbench the schema looks the same. So, why I'm getting this error?
Regards.
Upvotes: 0
Views: 127
Reputation: 601
As Daniel Roseman mentioned above, the code you might looking for is below, it does not need a new model:
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
If you do not mind to have another model, then inside your catalog.models you can add a new Bill model:
class Bill(models.Model):
application = models.ForeignKey('Application')
appcost = models.ForeignKey('AppCost')
tarification = models.ForeignKey('Tarification')
In your table file:
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
Upvotes: 1
Reputation: 599450
You're very confused about how to use django-tables. You need to specify one model in the Meta class, then just the fields
attribute to add a list of fields from that model, as strings, to display. You can't just specify fields from three arbitrary models.
Upvotes: 1