python-3.x Django错误:apps.get_models()返回空,无法进行查询或对模型执行任何操作

hk8txs48  于 2023-10-21  发布在  Python
关注(0)|答案(1)|浏览(209)

更新:这个问题似乎很简单,Django找不到我的应用,因为尝试运行app.get_app_config('mainpage')返回:
没有安装标签为“mainpage”的应用程序。
更新2:我做了一些调查,apps.get_models()返回的是完全空的!这是同一问题的另一个症状,让我想知道为什么Django什么都找不到。
然而,问题的其余部分仍然相同。我看不出为什么应用程序不应该被识别。
我在Django中遇到了上述错误。Before you yell at me, I have extensively read through this post on the same error..然而,那里提供的解决方案都没有帮助解决这个问题。
从本质上讲,我试图为我的每个模型创建一个示例,这样我就可以填充我的网页。然而,我甚至不能运行我的models.py没有上述错误,它看起来像这样:

from django.db import models

class HomePage(models.Model):
    name = models.CharField(max_length=200)

    def __str__(self):
        return self.name

class Section(models.Model):
    name = models.CharField(max_length=200)
    home_page = models.ForeignKey('mainpage.HomePage', on_delete=models.CASCADE)

    def __str__(self):
        return self.name

class NavBar(Section):
    logo = models.ImageField()

class Entry(models.Model):
    primary = models.BooleanField()
    text = models.CharField(max_length=15)

class Landing(Section):
    tagline = models.TextField()
    button_text = models.CharField(max_length=25)

class About(Section):
    title = models.CharField(max_length=200)

class Blurb(models.Model):
    "Blurb for an about section"
    title = models.CharField(max_length=200)
    body = models.TextField()
    section = models.ForeignKey('mainpage.About', on_delete=models.CASCADE)

class Capabilities(Section):
    title = models.CharField(max_length=200)
    description = models.TextField()
    button_text = models.CharField(max_length=40)

class Capability(models.Model):
    """Capability entry"""
    image = models.ImageField()
    description = models.TextField()
    section = models.ForeignKey('mainpage.Capabilities', on_delete=models.CASCADE)

class Testimonials(Section):
    title = models.CharField(max_length=200)

class Testimonial(models.Model):
    """Testimonial card"""
    name = models.CharField(max_length=200)
    role = models.CharField(max_length=200)
    description = models.TextField()

class Contact(Section):
    logo = models.ImageField()
    description = models.TextField()
    address = models.ForeignKey('mainpage.Address', on_delete=models.CASCADE)
    info = models.ForeignKey('mainpage.ContactInfo', on_delete=models.CASCADE)

class Address(models.Model):
    title = models.CharField(max_length=100)
    street_address = models.CharField(max_length=200)
    city = models.CharField(max_length=200)
    state = models.CharField(max_length=2)
    zip_code = models.IntegerField()

class ContactInfo(models.Model):
    title = models.CharField(max_length=100)
    phone = models.IntegerField()
    email = models.EmailField

class IconLink(models.Model):
    icon = models.CharField(max_length=50)
    link = models.TextField()
    contact_section = models.ForeignKey('mainpage.Contact', on_delete=models.CASCADE)

我安装的应用程序似乎很好:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'mainpage', # needs to be under contentypes (foreign key)
    'blog',
]

我的应用配置也是如此:

from django.apps import AppConfig

class MainpageConfig(AppConfig):
    name = 'mainpage'
    verbose_name = 'Main Page'

所以我真的很迷茫。这一切都很可悲,我得到了同样的错误,试图将模型导入到另一个文件:

import os, sys
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'gainsite.settings')
import django
from django.conf import settings
try:
    settings.configure()
except Exception as e:
    print(e)
django.setup()
from mainpage.models import Blurb

print(Blurb().__dict__)

我相当肯定Django没有正确地解析到我的模型的路径,但我是一个初学者,真的不知道足够的进一步研究。我查看了添加这些模型的迁移,看起来很不错:

from django.db import migrations, models
import django.db.models.deletion

class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Address',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('title', models.CharField(max_length=100)),
                ('street_address', models.CharField(max_length=200)),
                ('city', models.CharField(max_length=200)),
                ('state', models.CharField(max_length=2)),
                ('zip_code', models.IntegerField()),
            ],
        ),
        migrations.CreateModel(
            name='Blurb',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('title', models.CharField(max_length=200)),
                ('body', models.TextField()),
            ],
        ),
        migrations.CreateModel(
            name='Capability',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('image', models.ImageField(upload_to='')),
                ('description', models.TextField()),
            ],
        ),
        migrations.CreateModel(
            name='ContactInfo',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('title', models.CharField(max_length=100)),
                ('phone', models.IntegerField()),
            ],
        ),
        migrations.CreateModel(
            name='Entry',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('primary', models.BooleanField()),
                ('text', models.CharField(max_length=15)),
            ],
        ),
        migrations.CreateModel(
            name='HomePage',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=200)),
            ],
        ),
        migrations.CreateModel(
            name='IconLink',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('icon', models.CharField(max_length=50)),
                ('link', models.TextField()),
            ],
        ),
        migrations.CreateModel(
            name='Section',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=200)),
            ],
        ),
        migrations.CreateModel(
            name='Testimonial',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=200)),
                ('role', models.CharField(max_length=200)),
                ('description', models.TextField()),
            ],
        ),
        migrations.CreateModel(
            name='About',
            fields=[
                ('section_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='mainpage.Section')),
                ('title', models.CharField(max_length=200)),
            ],
            bases=('mainpage.section',),
        ),
        migrations.CreateModel(
            name='Capabilities',
            fields=[
                ('section_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='mainpage.Section')),
                ('title', models.CharField(max_length=200)),
                ('description', models.TextField()),
                ('button_text', models.CharField(max_length=40)),
            ],
            bases=('mainpage.section',),
        ),
        migrations.CreateModel(
            name='Contact',
            fields=[
                ('section_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='mainpage.Section')),
                ('logo', models.ImageField(upload_to='')),
                ('description', models.TextField()),
                ('address', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='mainpage.Address')),
                ('info', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='mainpage.ContactInfo')),
            ],
            bases=('mainpage.section',),
        ),
        migrations.CreateModel(
            name='Landing',
            fields=[
                ('section_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='mainpage.Section')),
                ('tagline', models.TextField()),
                ('button_text', models.CharField(max_length=25)),
            ],
            bases=('mainpage.section',),
        ),
        migrations.CreateModel(
            name='NavBar',
            fields=[
                ('section_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='mainpage.Section')),
                ('logo', models.ImageField(upload_to='')),
            ],
            bases=('mainpage.section',),
        ),
        migrations.CreateModel(
            name='Testimonials',
            fields=[
                ('section_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='mainpage.Section')),
                ('title', models.CharField(max_length=200)),
            ],
            bases=('mainpage.section',),
        ),
        migrations.AddField(
            model_name='section',
            name='home_page',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='mainpage.HomePage'),
        ),
        migrations.AddField(
            model_name='iconlink',
            name='contact_section',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='mainpage.Contact'),
        ),
        migrations.AddField(
            model_name='capability',
            name='section',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='mainpage.Capabilities'),
        ),
        migrations.AddField(
            model_name='blurb',
            name='section',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='mainpage.About'),
        ),
    ]

你们觉得呢?我已经在这件事上纠结了好几个小时了,这让我很沮丧。

tct7dpnv

tct7dpnv1#

我有多个设置,如本地生产和开发。我不知道我需要指定我在每个终端命令中使用的设置。当我这样做的时候,问题就解决了。我猜想,当django没有正确的设置文件时,它会将已安装的应用程序列表视为空。由于它没有安装的应用程序列表,当我指定一个正确的设置文件,它的工作。

相关问题