模型未显示django admin 2.1

时间:2019-02-19 12:40:21

标签: django django-models django-admin

如果我无法弄清楚为什么我的模型没有显示在管理员中,我该死。我正在开发一个名为btre_project的项目,这个问题困扰着我,请帮忙找出解决方案

列表应用中的

models.py:

from django.db import models
from datetime import datetime
from realtors.models import Realtor

class Listing(models.Model):
    realtor=models.ForeignKey(Realtor,on_delete=models.DO_NOTHING)
    models.CharField(max_length=100)
    state=models.CharField(max_length=100)
    zipcode=models.CharField(max_length=20)
    description=models.TextField(blank=True)
    price=models.IntegerField()
    bedrooms=models.IntegerField()
    bathrooms=models.DecimalField(max_digits=2,decimal_places=1)
    garage=models.IntegerField()
    sqft=models.IntegerField()
    lot_size=models.DecimalField(max_digits=5,decimal_places=1)
    photo_main=models.ImageField(upload_to='photos/%Y/%m/%d/')
    photo_1=models.ImageField(upload_to='photos/%Y/%m/%d/',blank=True)
    photo_2=models.ImageField(upload_to='photos/%Y/%m/%d/',blank=True)
    photo_3=models.ImageField(upload_to='photos/%Y/%m/%d/',blank=True)
    photo_4=models.ImageField(upload_to='photos/%Y/%m/%d/',blank=True)
    photo_5=models.ImageField(upload_to='photos/%Y/%m/%d/',blank=True)
    photo_6=models.ImageField(upload_to='photos/%Y/%m/%d/',blank=True)
    is_published=models.BooleanField(default=True)
    list_date=models.DateTimeField(default=datetime.now,blank=True)
    def __str__(self):
        return self.title

admin.py列表应用程序:

from django.contrib import admin
from .models import Listing


class ListingAdmin(admin.ModelAdmin):
  list_display = ('id', 'title', 'is_published', 'price', 'list_date', 
  'realtor')
  list_display_links=('id','title')
  list_filter=('realtor',)

admin.site.register(Listing)

项目settings.py

INSTALLED_APPS = [
'pages.apps.PagesConfig',
'listings.apps.ListingsConfig',
'realtors.apps.RealtorsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

]

urls.py:

from django.contrib import admin
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static


urlpatterns = [
    path('',include('pages.urls')),
    path('listings/',include('listings.urls')),
    path('admin/', admin.site.urls),
] + static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

帮我找出解决方案

3 个答案:

答案 0 :(得分:0)

也尝试注册admin.site.register(Listing, ListingAdmin)之类的ListingAdmin,而不是尝试仅注册模型类

答案 1 :(得分:0)

ListingAdmin未在admin.site.register()中注册

答案 2 :(得分:0)

问题通过使用related_name = profile解决