简体   繁体   English

数据必须像

[英]Data must be query-set like

I'm using an external database with Django. 我在Django中使用外部数据库。 I had already written a script to populate the database. 我已经写了一个脚本来填充数据库。 I was able to access this data with syncdb, and I have created a model for this data. 我可以使用syncdb访问此数据,并且已经为该数据创建了一个模型。

I am able to print the entire database, but using: 我能够打印整个数据库,但是使用:

 TicketOdds.objects.all()[0]

raises the following exception: 引发以下异常:

ValueError at / /的ValueError

data must be QuerySet-like (have count and order_by) or support list(data) -- TicketOdds has neither 数据必须类似于QuerySet(具有count和order_by)或支持列表(数据)-TicketOdds既没有

My model is: 我的模型是:

class TicketOdds(models.Model):
    #id = models.AutoField(primary_key=True)
    price = models.IntegerField(blank=True, null=True)
    ticket_name = models.TextField(blank=True, null=True)
    ticket_id = models.IntegerField(primary_key=True, blank=True, null=False)
    odds = models.FloatField(blank=True, null=True)
    img_url = models.TextField(blank=True, null=True)
    ticket_url = models.TextField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'ticket_odds'

    def __iter__(self):
        for i in xrange(100):
            yield i

    def __getitem__(self):
        return unicode(self)

What do I add to the model to make it "queryset-like"? 我要向模型添加什么以使其类似于“查询集”? I tried adding 我尝试添加

def __iter__ (self):

for that specific purpose... I must be missing something. 为了那个特定的目的...我一定会错过一些东西。

Since you didn't post the full traceback error message. 由于您没有发布完整的追溯错误消息。 I will just guide you along with the process to answer your question. 我将指导您完成回答您的问题的过程。

  1. Look at the source code . 看一下源代码 It will raise such error when data in the TableData(data, table) is not iterable. 它会提出这样的错误,当dataTableData(data, table)不迭代。 ie __iter__ is not defined. __iter__未定义。
  2. I doubt that you need to define the __iter__ methods in your Model or Model.Meta , because QuerySet which is an iterable class can be retrieved when you made this call: TicketOdds.objects.all() 我怀疑您是否需要在ModelModel.Meta定义__iter__方法,因为在进行此调用时可以检索作为可迭代类的QuerySetTicketOdds.objects.all()
  3. Pass the data with the queryset you retrieved previously when you make the TableData(data, table) 制作TableData(data, table)时,使用先前检索的查询集传递data

Example: 例:

TableData(TicketOdds.objects.all(), table)

What do I add to the model to make it "queryset-like"? 我要向模型添加什么以使其类似于“查询集”?

You don't make your model "queryset-like", but rather you get the QuerySet from the models using the above example. 您不必使模型“类似于Queryset”,而是可以使用上面的示例从模型中获取QuerySet

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM