【问题标题】:Models in Django are not showingDjango 中的模型未显示
【发布时间】:2022-01-22 23:21:54
【问题描述】:

我正在尝试在 django admin 中添加另一个模型,称为 Posts。我几乎没有其他正在工作和显示的模块,但我不知道为什么这个新模块没有出现。下面是我的代码

模型.py

from django.db import models
import datetime
from django.contrib.auth.models import User

STATUS = ((0, "Draft"), (1, "Published"))


class Post(models.Model):
    title = models.CharField(max_lenght=1048)
    slug = models.SlugField(max_lenght=1048)
    author = models.ForeignKey(User, on_delete=models.CASCADE,
                               related_name='blog_posts')
    content = models.TextField()
    status = models.IntegerField(choices=STATUS, default=0)

    class Meta:
        ordering = ['-created_on']

    def __str__(self):
        return self.title

admin.py

from django.contrib import admin
from .models import Post, Application, Message, FloaterMessage

# Register your models here.
admin.site.register(Post)
admin.site.register(Message)
admin.site.register(FloaterMessage)
admin.site.register(Application)

settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'visionStudios',
    'contact',
    'crispy_forms'
]

我认为这是maigration

    from django.db import migrations, models

    from django.contrib.auth.models import User

    STATUS = ((0,  "Draft"), (1, "Published"))

    class Migration(migrations.Migration):

initial = True

dependencies = [
    ('contact'),
]

operations = [
    migrations.CreateModel(
        name='Post',
        fields=[
            ('title',  models.CharField(max_lenght=1048)),
            ('slug', models.SlugField(max_lenght=1048)),
            ('author', models.ForeignKey(User, on_delete=models.CASCADE, related_name='blog_posts')),
            ('content', models.TextField()),
            ('status', models.IntegerField(choices=STATUS, default=0)),
        ],
    ),
]

我想在 Django admin 中显示

【问题讨论】:

  • “显示”具体在哪里……?
  • 你迁移了吗??
  • 如何正确迁移?只需添加一件事,我使用 digitalocean 作为服务器
  • ./manage.py migrate。如果其他模型正在显示(我猜是在管理员中),那么您已经完成了一次。顺便说一句,您是否已将应用添加到 INSTALLED_APPS
  • 应用已安装,名为contact

标签: python html django django-models


【解决方案1】:

如果您的应用正在开发中并且您不想丢失数据库中的数据,只需删除包含应用的“发布”模型中的数据库和迁移文件夹,然后运行python manage.py makemigrations <app name here> 然后python manage.py migrate <app name here>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-14
    • 2021-09-14
    • 2016-11-19
    • 2013-08-28
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    相关资源
    最近更新 更多