AttributeError:“postComments”对象没有“属性”模型

2024-04-18 20:23:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经为我的博客网站制作了一个新的模式postComments,现在我面临着上述问题:

型号.py

from django.db import models
from django.contrib.auth.models import User
from django.utils.timezone import now
# Create your models here.


class post(models.Model):
    SrNo = models.AutoField(primary_key=True)
    title = models.CharField(max_length=50)
    content = models.TextField()
    author = models.CharField(max_length=50)
    slug = models.SlugField(max_length=200)
    timeStamp = models.DateTimeField(blank=True)

    def __str__(self):
        return self.title + " by " + self.author


class postComments(models.Model):
    sno = models.AutoField(primary_key=True)
    comment = models.TextField()
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    post = models.ForeignKey(post, on_delete=models.CASCADE)
    parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True)
    timestamp = models.DateTimeField(default=now)

在写入上述“postComments”模型后,日志窗口显示以下错误:

错误日志

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Python38\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Python38\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\jayant nigam\projects\practise\lib\site-packages\django\utils\autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\jayant nigam\projects\practise\lib\site-packages\django\core\management\commands\runserver.py", line 118, in inner_run  
    self.check(display_num_errors=True)
  File "C:\Users\jayant nigam\projects\practise\lib\site-packages\django\core\management\base.py", line 392, in check
    all_issues = checks.run_checks(
  File "C:\Users\jayant nigam\projects\practise\lib\site-packages\django\core\checks\registry.py", line 70, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "C:\Users\jayant nigam\projects\practise\lib\site-packages\django\contrib\admin\checks.py", line 54, in check_admin_app
    errors.extend(site.check(app_configs))
  File "C:\Users\jayant nigam\projects\practise\lib\site-packages\django\contrib\admin\sites.py", line 84, in check
    if modeladmin.model._meta.app_config in app_configs:
AttributeError: 'postComments' object has no attribute 'model'

admin.py

from django.contrib import admin
from blog.models import post, postComments

# Register your models here.

admin.site.register((post), (postComments))

url.py(博客应用程序)

from django.urls import path, include
from blog import views

urlpatterns = [
    path('', views.blogHome, name='home'),
    path('<str:slug>', views.blogPost, name='blogpost'),
    # here blog_name is string variable which will take the input after /blog/anyblogname
    # and after this the page will display: this is 'anyblogname'
]

在添加这个新模型之前,这个网站运行得很好


Tags: djangoinfrompyimportselfmodelslib
1条回答
网友
1楼 · 发布于 2024-04-18 20:23:33

注册模型的正确方法是每次注册调用一个

from django.contrib import admin 
from blog.models import post, postComments 

 # Register your models here.  

admin.site.register(post)
admin.site.register(postComments)

相关问题 更多 >