.Manager model error

Model: http://dpaste.com/96349/

View:

def game_list(request):
        return render_to_response('games/game_list.html',
                                  { 'object_list': GameList.objects.published.all().order_by('position')})

      

Template:

AttributeError at / games / The "Manager" object has no attribute "published"

It would seem that I really dislike my new manager?

+2


source to share


1 answer


If you are trying to use the published manager instead of the object manager, you must remove the object reference from the filtering process. In addition, the published manager is declared for the Game model, not the GameList model. You will probably have to refactor how this works.

Edit: here's something that might be similar to what you are trying to do.

from django.db import models

class GamePublishedManager(models.Manager):
 use_for_related_fields = True
 def get_query_set(self):
  return super(GamePublishedManager, self).get_query_set().filter(game__status='p')

STATUS_CHOICES = (
    ('d', 'Draft'),
    ('p', 'Published'),
    ('w', 'Withdrawn'),
)

class Game(models.Model):
 name = models.CharField(max_length=200)
 status = models.CharField(max_length=1, choices=STATUS_CHOICES)

 def __unicode__(self):
  return self.name

class GameList(models.Model):
 game = models.ForeignKey(Game)
 position = models.IntegerField()
 objects = models.Manager()
 published = GamePublishedManager()

 def __unicode__(self):
  return self.game.name

      



Your new manager filter has been modified to reference the appropriate game status, and the manager has been linked to the GameList model instead of the game. Now the command to use will be as follows:

GameList.published.all().order_by('position')

      

+3


source







All Articles