AtributeError: 'NoneType' object has no atribute '_meta'

Hello everyone,
I am now in part 7 of the tutorial.
After following through all of that part, now I am I getting this error.
It comes from this file:

...\env\lib\site-packages\django\contrib\admin\options.py line 2305, in

__init__
    self.opts = self.model._meta

Thank you!

Part 7 is where you’re working on the admin.py file, right? If so, please post your admin.py file here.

Yes you are right.
polls/admin.py:

from django.contrib import admin
from .models import Question, Choice
class ChoiceInline(admin.StackedInline):
    model = Choice
    extra = 3

class QuestionAdmin(admin.TabularInline):
    fieldsets = [
        (None,    {'fileds': ['question_text']}),
        ('Date information', {'fileds': ['pub_date'], 'class': ['collapse']}),
    ]
    inlines = [ChoiceInline]
    list_display = ('question_text', 'pub_date', 'was_published_recently')
    list_filter = ['pub_date']
    search_fields = ['question_text']

adkin.site.register(Question, QuestionAdmin)
admin.site.register(Choice)

You have at least two typos. Compare carefully what you have with what’s in the second example at Writing your first Django app, part 7 | Django documentation | Django

My bad.
fields and classes
I have checked after updating but it doesnt solve the error.
The error does not come from the file admin.py

The final error may not - but the root issue will be something that isn’t right in your code. It may be helpful if you posted the complete traceback.

Also, this part of the tutorial involves changes to your Question model, so please post your current Question model .

Sorry for the late reply, I just managed to sit around srable internet.
This is the full traceback:

(env) C:\Users\omarb\Desktop\267\mysite>python manage.py runserver
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Users\omarb\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "C:\Users\omarb\AppData\Local\Programs\Python\Python310\lib\threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\omarb\Desktop\267\env\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\omarb\Desktop\267\env\lib\site-packages\django\core\management\commands\runserver.py", line 125, in inner_run
    autoreload.raise_last_exception()
  File "C:\Users\omarb\Desktop\267\env\lib\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception
    raise _exception[1]
  File "C:\Users\omarb\Desktop\267\env\lib\site-packages\django\core\management\__init__.py", line 398, in execute
    autoreload.check_errors(django.setup)()
  File "C:\Users\omarb\Desktop\267\env\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\omarb\Desktop\267\env\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\omarb\Desktop\267\env\lib\site-packages\django\apps\registry.py", line 124, in populate
    app_config.ready()
  File "C:\Users\omarb\Desktop\267\env\lib\site-packages\django\contrib\admin\apps.py", line 27, in ready
    self.module.autodiscover()
  File "C:\Users\omarb\Desktop\267\env\lib\site-packages\django\contrib\admin\__init__.py", line 50, in autodiscover
    autodiscover_modules("admin", register_to=site)
  File "C:\Users\omarb\Desktop\267\env\lib\site-packages\django\utils\module_loading.py", line 58, in autodiscover_modules
    import_module("%s.%s" % (app_config.name, module_to_search))
  File "C:\Users\omarb\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "C:\Users\omarb\Desktop\267\mysite\polls\admin.py", line 19, in <module>
    admin.site.register(Question, QuestionAdmin)
  File "C:\Users\omarb\Desktop\267\env\lib\site-packages\django\contrib\admin\sites.py", line 149, in register
    self._registry[model] = admin_class(model, self)
  File "C:\Users\omarb\Desktop\267\env\lib\site-packages\django\contrib\admin\options.py", line 2305, in __init__
    self.opts = self.model._meta
AttributeError: 'NoneType' object has no attribute '_meta'

We still need to see your updated / corrected admin.py file along with your models.py file.

Question model:

class Question(model.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def __str__(self):
        return self.question_text
    @admin.display(
        boolean=True,
        ordering = 'update',
        description = 'Published recently',
    )
    def was_published_recently(self):
        now = timezone.now()
        return now - datetime.timedelta(days=1) <= self.pub_date <= now

admin.py

from django.contrib import admin

# Register your models here.
from .models import Question, Choice

class ChoiceInline(admin.StackedInline):
    model = Choice
    extra = 3

class QuestionAdmin(admin.TabularInline):
    filedsets = [
        (None,      {'fields': ['question_text']}),
        ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
    ]
    inlines = [ChoiceInline]
    list_display = ('question_text', 'pub_date', 'was_published_recently')
    list_filter = ['pub_date']
    search_fields = ['question_text']
admin.site.register(Question, QuestionAdmin)
admin.site.register(Choice)

modules.py

import datetime
from email.policy import default
from pydoc import describe
from django.db import models
from django.utils import timezone
# Create your models here.
from django.contrib import admin


class Question(models.Model):
    
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def __str__(self):
        return self.question_text
    @admin.display(
        boolean=True,
        ordering = 'pubdate',
        description = 'Published recently',
    )
    def was_published_recently(self):
        now = timezone.now()
        return now - datetime.timedelta(days=1) <= self.pub_date <= now

class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete = models.CASCADE)
    choice_text =  models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
    def __str__(self):
        return self.choice_text

def was_published_recently(self):
    now = timezone.now()
    return now - datetime.timedelta(days=1)<= self.pub_date<=now

You still have multiple typos between your models.py and your admin.py.

You need to compare your code carefully with the tutorial.

admin.py

from django.contrib import admin

# Register your models here.
from .models import Question, Choice

class ChoiceInline(admin.StackedInline):
    model = Choice
    extra = 3

class QuestionAdmin(admin.TabularInline):
    filedsets = [
        (None,      {'fields': ['question_text']}),
        ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
    ]
    inlines = [ChoiceInline]
    list_display = ('question_text', 'pub_date', 'was_published_recently')
    list_filter = ['pub_date']
    search_fields = ['question_text']
admin.site.register(Question, QuestionAdmin)
admin.site.register(Choice)

models.py

import datetime
from email.policy import default
from pydoc import describe
from django.db import models
from django.utils import timezone
# Create your models here.
from django.contrib import admin


class Question(models.Model):
    
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def __str__(self):
        return self.question_text
    @admin.display(
        boolean=True,
        ordering = 'pub_date',
        description = 'Published recently?',
    )
    def was_published_recently(self):
        now = timezone.now()
        return now - datetime.timedelta(days=1) <= self.pub_date <= now

class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete = models.CASCADE)
    choice_text =  models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
    def __str__(self):
        return self.choice_text

def was_published_recently(self):
    now = timezone.now()
    return now - datetime.timedelta(days=1)<= self.pub_date<=now

I think I carefully looked at the instruction and typed it as it should be.

Is there still an issue?

I still see two mistakes in your admin.py file.

Could you quote them for me please?
Yes the issue is still there.

I could, but ultimately that doesn’t help you develop that “critical eye for detail” that is such an important part of programming.

Being able to compare two sections of code to visibly locate and identify material differences is a skill you will use a lot as a developer.

1 Like

Tusen Takk!
Now it’s running.
Any idea where to go from here?
Or I should finnish the advanced concepts first?