Home > Software design >  The model is not displayed in the django admin panel
The model is not displayed in the django admin panel

Time:02-04

I don't have the advertisement module displayed in the django admin panel. Here is the model code

from django.db import models


class Advertisement(models.Model):
    title = models.CharField(max_length=1000, db_index=True)
    description = models.CharField(max_length=1000, default='', verbose_name='description')
    creates_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    price = models.FloatField(default=0, verbose_name="price")
    views_count = models.IntegerField(default=1, verbose_name="views count")
    status = models.ForeignKey('AdvertisementStatus', default=None, null=True, on_delete=models.CASCADE,
                               related_name='advertisements')

    def __str__(self):
        return self.title

    class Meta:
        db_table = 'advertisements'
        ordering = ['title']


class AdvertisementStatus(models.Model):
    name = models.CharField(max_length=100)

admin.py /

from django.contrib import admin
from .models import Advertisement

admin.site.register(Advertisement)

I was just taking a free course from YouTube. This was not the case in my other projects. Here I registered the application got the name in INSTALLED_APPS. Then I performed the creation of migrations and the migrations themselves. Then I tried to use the solution to the problem  in browser

console  in pycharm

CodePudding user response:

admins.py

The name of the file is admin.py not admins.py. Yes, that is a bit confusing since most module names in Django are plural. The rationale is probably that you define a (single) admin for the models defined.

Alternatively, you can probably force Django to import this with the AppConfig:

# app_name/apps.py

from django.apps import AppConfig


class AppConfig(AppConfig):
    def ready(self):
        # if admin definitions are not defined in admin.py
        import app_name.admins  # noqa

CodePudding user response:

There could be a few reasons why the model is not being displayed in the Django admin panel. Some possible reasons are:

Check if you have the correct name in the INSTALLED_APPS setting in your settings.py file. The name should match the name of your Django app.

Make sure that the Django app is included in the URL configuration.

Check if the admin.py file is located in the right location in your Django app and that it's imported correctly.

Try restarting the development server after making any changes to the code.

If the issue still persists, please share the complete traceback of the error, if any, or any additional relevant information to help diagnose the issue.

  • Related