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
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