Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/Django AttributeError "Object 'players' has no attribute 'fields'

I am setting up the admin page so that I might be able to use it to add data, players in this case. When you go and attempt to register the Players class in admin.py you get the error described in the question title (object 'players' has no attribute 'fields'). Looking through views.py that I pasted a snippet out of below I don't see what it might be referring to.

Sorry if this is a noob question, I'm pretty new to both django and python.

class Players(models.Model):
    player_id        = models.IntegerField(primary_key=True)
    firstname        = models.CharField(max_length=50)
    lastname         = models.CharField(max_length=50)
    team             = models.ForeignKey(Teams)
    Top200rank       = models.IntegerField(null=True, blank=True)
    position         = models.CharField(max_length=25)
    roster_status    = models.ForeignKey(RosterStatus, null=True, blank=True)
    class Meta:
        ordering = ('lastname', 'firstname')
        verbose_name_plural = 'Players'

    def __unicode__(self):
        return u"%s %s" % (self.firstname, self.last_name)
like image 305
CaleyWoods Avatar asked Sep 01 '10 16:09

CaleyWoods


2 Answers

Fix was in admin.py, see below.

Before:

#foozball admin python file

from foozball.leaguemanager.models import Division, Games, RosterStatus, Teams, Players
from django.contrib import admin

admin.site.register(Teams, Players)

After:

foozball admin python file

from foozball.leaguemanager.models import Division, Games, RosterStatus, Teams, Players
from django.contrib import admin

admin.site.register(Teams)
admin.site.register(Players)
like image 197
CaleyWoods Avatar answered Oct 13 '22 13:10

CaleyWoods


The specific error you're getting is important:

Request Method: GET
Request URL:    http://127.0.0.1:8000/admin/
Exception Type: AttributeError
Exception Value:    
type object 'Player' has no attribute 'fields'

This error does come from the line,

admin.site.register(Teams, Players)

but more specifically comes from the fact that pairing two models as you have done means something rather specific, namely that the second object (here Players) is a model admin object associated with the first object (here Teams, always a Django model). A model admin object must have an attribute 'fields', which gives information about how an individual model's attribute fields are displayed when using the admin interface. Since Players is not a model admin object, and doesn't have an attribute named 'fields', Djagno raises an exception. Click here for more information.

A quick fix for this situation is as you've guessed, to separate them into different register calls:

admin.site.register(Teams)
admin.site.register(Players)
like image 45
smessing Avatar answered Oct 13 '22 12:10

smessing