Carl Gueck
Carl Gueck

Reputation: 31

Data must be query-set like

I'm using an external database with 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.

I am able to print the entire database, but using:

 TicketOdds.objects.all()[0]

raises the following exception:

ValueError at /

data must be QuerySet-like (have count and order_by) or support list(data) -- TicketOdds has neither

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.

Upvotes: 2

Views: 671

Answers (1)

Yeo
Yeo

Reputation: 11784

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. i.e. __iter__ is not defined.
  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()
  3. Pass the data with the queryset you retrieved previously when you make the TableData(data, table)

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.

Upvotes: 2

Related Questions