Getting AttributeError while registering my model

2019-08-02 19:27发布

问题:

What i am trying to do:

Trying to registering my model to django admin

What problem do i get:

I am getting the following error:

My Code:

admin.py:

from django.contrib import admin
from .models import UserProfile, Post

admin.site.register(UserProfile,Post)

model.py:

from django.contrib.auth.models import User
from django.db import models

class UserProfile(models.Model):
    user = models.ForeignKey(User,on_delete=models.CASCADE)
    avatar = models.ImageField(upload_to='static/media/',max_length=100)

class Post(models.Model):
    title = models.CharField(max_length=120)
    content = models.TextField()

urls.py:

from django.conf.urls import url
from .import views

urlpatterns = [
    url(r'^register/$',views.registerUser),
    url(r'^$', views.index, name="Index"),
    url(r'^validateRegisterForm/$',views.validateRegisterForm),
    url(r'^validateLoginForm/$',views.validateLoginForm),
    url(r'^article/$', views.article, name="Article"),
    url(r'^Login/$',views.loginUser, name="Login"),
    url(r'^Logout/$',views.logoutUser, name="Logout"),
]

Note: I am new to django so, don't know much about it.

回答1:

This should be

admin.site.register(UserProfile)
admin.site.register(Post)

Admin.site.register takes two parameters, the first is the model class and the optional second parameter is an admin class. In your code, you were passing another model as the admin class.

for more information, please refer to :https://docs.djangoproject.com/en/1.10/ref/contrib/admin/