Reputation: 31
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
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.
data
in the TableData(data, table)
is not iterable. i.e. __iter__
is not defined. __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()
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