You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行Django makemigrations命令时报TypeError错误求助

问题排查与解决

错误核心原因

报错TypeError: issubclass() arg 1 must be a class的根源是**signals.py中手动调用settings.configure()破坏了Django的初始化流程**,导致AppConfig无法正常加载。此外代码中还有几处语法和逻辑错误需要修正。

分步修复方案

1. 修复signals.py

删除重复的settings.configure()调用,同时修正实例创建的方法与字段匹配:

from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.dispatch import receiver
from .models import Profile  # 对应后续模型类名修改

@receiver(post_save, sender=User)
def post_save_create_profile(sender, instance, created, **kwargs):
    print('sender', sender)
    print('instance', instance)
    if created:
        # 模型中OneToOneField字段为business_name,需传入User实例
        Profile.objects.create(business_name=instance)

2. 修正models.py的类名与字段逻辑

遵循Python类名大驼峰规范,同时修正DateTimeField参数(auto_now_add用于创建时赋值,auto_now用于更新时赋值,原代码参数颠倒):

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

class Profile(models.Model):  # 类名改为大驼峰Profile
    first_name = models.CharField(max_length=200, blank=True)
    last_name = models.CharField(max_length=200, blank=True)
    business_name = models.OneToOneField(User, on_delete=models.CASCADE)
    phone_num = models.CharField(max_length=200, blank=True)
    email = models.EmailField(max_length=200, blank=True)
    country = models.CharField(max_length=200, blank=True)
    state = models.CharField(max_length=200, blank=True)
    follow_BNS = models.ManyToManyField(User, related_name='othe_BNS', blank=True)
    D_p = models.ImageField(default='default.jpg', upload_to='profile_pic')
    create = models.DateTimeField(auto_now_add=True)  # 创建时自动赋值
    update = models.DateTimeField(auto_now=True)  # 更新时自动赋值

    def __str__(self):
        return f'{self.business_name.username}, {self.phone_num}'  # 显示用户名更直观

3. 更新admin.py的模型引用

对应修改为大驼峰的Profile类:

from django.contrib import admin
from .models import Profile

admin.site.register(Profile)

4. 检查INSTALLED_APPS配置

确认INSTALLED_APPS中的share、hitcount、post、location等app已正确创建并配置,若未完成初始化,建议暂时注释,先保证profilesapp正常运行:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # 'share',  # 未创建则暂时注释
    # 'hitcount',
    # 'post',
    'profiles',
    # 'location',
]

验证修复

完成上述修改后,重新执行命令:

python manage.py makemigrations
python manage.py migrate

内容的提问来源于stack exchange,提问作者yy.chazali

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 23:09:33