djangodjango-voting

Django converting generator to list


I am using django-voting package and have been trying to get it's manager get_top() to work. I've stumbled upon one problem - it produces generator (from which actually I need to extract data to select items from database on) which seems to be a problem for me.

After spending two days of googling and reading forums, the closest think I came to was this: What is "generator object" in django?

It says that any generator can be converted to list by:

mylist=list(myGenerator)

Althought if I do convert generator to list I get the following error:

'NoneType' object has no attribute '_meta'

Here is my view and model code:

def main(request):
    temporary = TopIssue.objects.get_top(Model=Issue, limit=10)
    temp_list = list(temporary)
    return render_to_response('main/index.html', temp_list)

from voting.managers import VoteManager
class TopIssue:
    objects = VoteManager()

Any ideas?


Solution

  • Maybe this is just a typo in your example code, but your class TopIssue is not derived from a Django model class. That could also explain why you get the error message about the missing _meta attribute.

    Edit: I'm not familiar with django-voting, but from skimming the docs, the first argument to the manager's get_top() function has to be a Django Model.

    You accomplish that by inheriting from a base class provided by Django. Django models are explained in the Django Model Documentation.

    Thus at the very least, your TopIssue class should be declared like this:

    from django.db import models
    
    class TopIssue(models.Model):
        # fields go here
        objects = VoteManager() # for integration with django-voting
    

    Your TopIssue class should be a database model, and the get_top() function is supposed to return the top voted instance of that model. Please post the rest of your code if you have further questions (it seems very strange to me if what you have posted is your complete TopIssue class -- you are missing fields, etc.).