【问题标题】:Object reference to django model - missing "object"对 django 模型的对象引用 - 缺少“对象”
【发布时间】: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'

标签: python django


【解决方案1】:

Summarize 是类型对象,但它需要是 Model 类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    • 1970-01-01
    • 2017-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    相关资源
    最近更新 更多