【发布时间】:2020-04-01 09:43:30
【问题描述】:
我的数据看起来像:Store Count_Customers Count_Purchases
BestBuy 2 2
Target 1 1
Target 4 3
Walmart 5 1
Target 9 1
我想使用 django 模型来生成商店列表,并按商店进行汇总。
所以我有 models.py 为:
class UniqueStore(models.Model):
store = models.CharField(db_column='store', max_length=100, unique=True)
class Summarize(object):
store = models.ForeignKey(UniqueStore,
on_delete=models.CASCADE, unique=True)
sum_customers = models.IntegerField(u"count_customers")
sum_purchases = models.IntegerField(u"count_purchases")
和 my_summary.py 作为管理命令一样
class Command(BaseCommand):
help = "Summary by store"
def handle(self, *args, **options):
print("Stores: {}".format(UniqueStore.objects.all().count()))
print("Facts: {}".format(Summarize.objects.all().sum()))
但在我运行 python manage.py my_summary 之前,我收到了错误unresolved attribute reference: object
能否帮助我了解如何解决此错误?
我没有在文档中看到这个问题:https://docs.djangoproject.com/en/3.0/topics/db/models/
如果它与元类(django model referencing object from other class)有关,有人可以提供额外的资源/解释吗?
【问题讨论】:
-
“但在我运行
python manage.py my_summary之前”是什么意思?您正在执行什么导致错误消息? -
IDE Pycharm 突出显示错误,然后是的,当我尝试运行该命令时,我得到
AttributeError: type object 'Summarize' has no attribute 'objects'