Jayant Nigam
Jayant Nigam

Reputation: 171

AttributeError: 'postComments' object has no attribute 'model

I have made a new model postComments for my blogging website and now I am facing the above issue:

models.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)

after writing the above '''postComments``` model the log window is showing me this error:

error log

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))

urls.py(blog app):

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'
]

before adding this new model, the website was working just fine.

Upvotes: 1

Views: 1190

Answers (1)

Alexey Popov
Alexey Popov

Reputation: 756

The correct way to register models is one per register call

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

 # Register your models here.  

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

Upvotes: 6

Related Questions