吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 778|回复: 8
收起左侧

[求助] DJnaog项目报错求大佬帮忙解决一下。

[复制链接]
feng0945 发表于 2023-6-8 17:03
django4.2.1项目前后端分离的时候在写数据库迁移以后写一个视图就报错。 Conflicting 'menu' models in application 'user': <class 'user.models.Menu'> and <class 'api_file.apps.user.models.Menu'>. 检查了很多地方就是没有发现错误,请大佬们帮忙看下。
一下是主要代码:
[Python] 纯文本查看 复制代码
views.py

from rest_framework.serializers import ModelSerializer
from api_file.apps.user import models
from rest_framework.viewsets import ModelViewSet


class ModelSerializer(ModelSerializer):
    class Meta:
        model = models.UserInfo
        fields = '__all__'


class user(ModelViewSet):
    queryset = models.UserInfo.all()
    serializer_class = ModelSerializer


[Python] 纯文本查看 复制代码
models.py

from django.db import models
from api_file.apps.user.basemodel import BaseModel


class Menu(BaseModel):
    title = models.CharField(verbose_name='菜单名称', max_length=32)

    class Meta:
        app_label = 'user'

    def __str__(self):
        return self.title


class Permission(BaseModel):
    title = models.CharField(verbose_name='标题', max_length=32)
    url = models.CharField(verbose_name='含正则的URL', max_length=128)
    name = models.CharField(verbose_name='URL别名', max_length=32, unique=True)
    menu = models.ForeignKey(verbose_name='所属菜单', to='Menu', null=True, blank=True,
                             help_text='null表示不是菜单;非null表示是二级菜单', on_delete=models.CASCADE)
    pid = models.ForeignKey(verbose_name='关联的权限', to='Permission', null=True, blank=True, related_name='children',
                            help_text='对于非菜单权限需要选择一个可以成为菜单的权限,用户做默认展开和选中菜单。',
                            on_delete=models.CASCADE)

    def __str__(self):
        return self.title


class Role(BaseModel):
    title = models.CharField(verbose_name='角色名称', max_length=32)
    permissions = models.ManyToManyField(verbose_name='拥有的所有权限', to='Permission', blank=True)

    def __str__(self):
        return self.title


class UserInfo(models.Model):
    name = models.CharField(verbose_name='用户名', max_length=32, unique=True)
    password = models.CharField(verbose_name='密码', max_length=64)
    email = models.CharField(verbose_name='邮箱', max_length=32, unique=True)
    phone = models.CharField(max_length=11, blank=False, unique=True)
    icon = models.ImageField(upload_to='icon', default='icon/default.png')
    roles = models.ManyToManyField(verbose_name='拥有的所有角色', to='Role', blank=True)

    def __str__(self):
        return self.name

    def get_all_permissions(self):
        """
        获取用户拥有的所有权限
        """
        permissions = set()
        for role in self.roles.all():
            permissions |= set(role.permissions.all())
        return permissions

    def get_menu_permissions(self):
        """
        获取用户拥有的菜单权限
        """
        menu_permissions = set()
        for permission in self.get_all_permissions():
            if permission.menu:
                menu_permissions.add(permission.menu)
                menu_permissions |= set(permission.menu.permission_set.all())
        return menu_permissions

    def get_menu_permission_urls(self):
        """
        获取用户拥有的菜单权限的URL
        """
        urls = set()
        for permission in self.get_all_permissions():
            if permission.menu:
                urls.add(permission.url)
                urls |= set(permission.menu.permission_set.values_list('url', flat=True))
        return urls


[Asm] 纯文本查看 复制代码
settings

# -*- coding: utf-8 -*-
# [url=home.php?mod=space&uid=238618]@Time[/url]    : 2023-6-8 0008 14:28
from pathlib import Path
import os
import sys

BASE_DIR = Path(__file__).resolve().parent.parent
sys.path.insert(0, BASE_DIR)
sys.path.insert(1, os.path.join(BASE_DIR, 'apps'))

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-24(&)#p*5gas=*ovcn-zzi!pz=wwx!u%qq(9#k-jh7(h4i)dc$'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'user',
    'menu',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'api_file.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates']
        ,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'api_file.wsgi.application'

# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'file_api',
        'USER': 'file',
        'PASSWORD': '',
    }
}
import pymysql

pymysql.install_as_MySQLdb()

# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Internationalization
# https://docs.djangoproject.com/en/4.2/topics/i18n/

LANGUAGE_CODE = 'zh-hans'

TIME_ZONE = 'Asia/shanghai'

USE_I18N = True

USE_TZ = False

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/

STATIC_URL = 'static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'


以上是代码,我把整个工程也打包上传网盘:链接:https://pan.baidu.com/s/1jPiOWFdQsb-PE89_WRPBEw?pwd=60bg
提取码:60bg
--来自百度网盘超级会员V7的分享

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

kk159 发表于 2023-6-8 17:35
原生user的问题应该
loveucrazy 发表于 2023-6-8 21:10
应该是你的 user.models.py 的Menu 和 menu 这个app冲突了
 楼主| feng0945 发表于 2023-6-9 09:08
loveucrazy 发表于 2023-6-8 21:10
应该是你的 user.models.py 的Menu 和 menu 这个app冲突了

我修改了  Menu的还是一样的报错。 Conflicting 'menutitle' models in application 'user': <class 'user.models.MenuTitle'> and <class 'api_file.apps.user.models.MenuTitle'>.
 楼主| feng0945 发表于 2023-6-9 09:09
kk159 发表于 2023-6-8 17:35
原生user的问题应该

我之前也是继承了原生的user也是报错,所以就没有用原生的模型,自己写的RBAC模型
kk159 发表于 2023-6-9 13:01
AUTH_USER_MODEL 这个改改试试
一闪一闪233 发表于 2023-6-10 03:48
[Python] 纯文本查看 复制代码
from rest_framework.serializers import ModelSerializer
from user import models   # 从'user'导入模型,而不是'api_file.apps.user'
from rest_framework.viewsets import ModelViewSet
 
 
class ModelSerializer(ModelSerializer):
    class Meta:
        model = models.UserInfo
        fields = '__all__'
 
 
class user(ModelViewSet):
    queryset = models.UserInfo.all()
    serializer_class = ModelSerializer
 楼主| feng0945 发表于 2023-6-12 09:52
一闪一闪233 发表于 2023-6-10 03:48
[mw_shl_code=python,true]from rest_framework.serializers import ModelSerializer
from user import mo ...

谢谢!大佬的确是你说的这个问题,我检查了一下因为在settings里面注册的apps这个环境变量。这里这样引用这样写引用就出现了循环引用的问题。
 楼主| feng0945 发表于 2023-6-12 09:52
kk159 发表于 2023-6-9 13:01
AUTH_USER_MODEL 这个改改试试

我没有用django自己的表继承,不用设置这个。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-24 22:29

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表