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)
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:
from foozball.leaguemanager.models import Division, Games, RosterStatus, Teams, Players
from django.contrib import admin
admin.site.register(Teams)
admin.site.register(Players)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With