'Django throws AttributeError: 'str' object has no attribute '_meta' when I register my Model

Im novice and I didnt find solution for this problem. Im writing code for a blog. I have a model Post where I want to create field author which is ForeighKey to User. If I make migration without admin.site.register - everything is ok. But then Im trying to register my Model to Django admin it throws AttributeError: 'str' object has no attribute '_meta'

Comment: my idea is that every user (after registration) can create post. And I want to show some information about author using User model

models.py

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


class Post(models.Model):
    author = models.ForeignKey(get_user_model(), on_delete=models.CASCADE, verbose_name='Автор', related_name='posts', null=True)
    title = models.CharField(verbose_name='Заголовок объявления', max_length=255)
    text = models.TextField(verbose_name='Текст объявления')
    CATEGORIES = (
        ('T','Танки'),
        ('HM','Хилы'),
        ('DD','ДД'),
        ('TrM','Торговцы'),
        ('GM','Гилдмастеры'),
        ('QG','Квестгиверы'),
        ('FM','Кузнецы'),
        ('LM','Кожевники'),
        ('PM','Зельевары'),
        ('WM','Мастера заклинаний')
    )

    category = models.CharField(verbose_name='Категория', max_length=3, choices=CATEGORIES)
    pub_date = models.DateTimeField(auto_now_add=True, verbose_name='Дата создания')

    class Meta:
        verbose_name = 'Пост'
        verbose_name_plural = 'Посты'

    def __str__(self):
        return self.title

admin.py

from django.contrib import admin
from .models import *

admin.site.register('Post')


Solution 1:[1]

You need to pass a reference to the class, not a string literal, so:

from django.contrib import admin
from app_name.models import Post

admin.site.register(Post)

where you replace app_name with the name of the app where you defined the Post model.


Note: Please do not use wildcard imports [quantifiedcode.com]. It makes the statement less predictable, it can easily result in failing code if you later decide to change what is exported in a certain module, and furthermore it can override variables.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1