我正在尝试使用django创建专门的会员资格应用程序。我在规划应用程序+表结构和数据流时遇到了一些麻烦。例如:我有基本的django用户模型。我正在使用一个名为“ allauth”的程序包,并创建了一个包含基本人员信息的“个人资料”模型。我将所有内容捆绑在一起,可以用于注册,帐户验证,注销和访问原始的“用户页面”。下一步是我会迷路并遇到导入错误。
创建登录的人必须年满18岁。该人可以是成年成员,也可以是一个或多个青年成员的父母,也可以是父母和成年成员。在任何情况下,都认为已登录的人员对与其相关的会员帐户负有财务责任。在某些情况下,一个以上的登录人员(如配偶)可以在他们之间共享多个“帐户”。例如,成年夫妻中的任何一个人都可能在财务上负责支付账单。
因此,对于给定的登录“配置文件”,我需要一个与“帐户”表的ManyToMany
关系。一个帐户可以与一个或多个成员记录具有ManyToMany
关系,并且成员“记录”应该位于“个人资料”表中,但可能在“用户”表中没有登录记录。
现在我们来解决我的问题。使用allauth
配置和account_adaptor
方法,我将django“用户”模型附加到帐户配置文件中,即la :(为简洁起见而被修剪)
# PROFILE
import uuid
from auditlog.models import AuditlogHistoryField
from auditlog.registry import auditlog
from django.contrib.auth.models import User
from django.db import models
from .account import Account
class Profile(models.Model):
id = models.UUIDField(
max_length=32,
default=uuid.uuid4,
editable=False,
primary_key=True,
blank=False,
null=False,
)
user = models.OneToOneField(
User,
on_delete=models.CASCADE,
unique=True,
blank=True,
null=True,
)
name_info = ...
birthdate = models.DateField(
blank=False,
null=False
)
address_info = ...
# ACCOUNT
import uuid
from auditlog.models import AuditlogHistoryField
from auditlog.registry import auditlog
from django.db import models
from .student import Student
from Billing.models.payment import Payment
class Account(models.Model):
id = models.UUIDField(
max_length=32,
default=uuid.uuid4,
editable=False,
primary_key=True,
blank=False,
null=False,
)
students = models.ManyToManyField(Student)
payments = models.ManyToManyField(Payment)
# STUDENT
import uuid
from auditlog.models import AuditlogHistoryField
from auditlog.registry import auditlog
from django.db import models
from .profile import Profile
class Student(models.Model):
id = models.UUIDField(
max_length=32,
default=uuid.uuid4,
editable=False,
primary_key=True,
blank=False,
null=False,
)
profile = models.OneToOneField(
Profile,
on_delete=models.CASCADE,
unique=True,
blank=False,
null=False,
)
当我做makemigrations
时,我得到了堆栈跟踪:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "C:\me\myProject\venv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
utility.execute()
File "C:\me\myProject\venv\lib\site-packages\django\core\management\__init__.py", line 357, in execute
django.setup()
File "C:\me\myProject\venv\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\me\myProject\venv\lib\site-packages\django\apps\registry.py", line 112, in populate
app_config.import_models()
File "C:\me\myProject\venv\lib\site-packages\django\apps\config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "C:\Program Files (x86)\Python3-6-5\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\me\myProject\myApp\Manager\models\__init__.py", line 2, in <module>
from .group_list import *
File "C:\me\myProject\myApp\Manager\models\group_list.py", line 5, in <module>
from Members.models.profile import Profile
File "C:\me\myProject\myApp\Members\models\__init__.py", line 1, in <module>
from .profile import *
File "C:\me\myProject\myApp\Members\models\profile.py", line 8, in <module>
from .account import Account
File "C:\me\myProject\myApp\Members\models\account.py", line 7, in <module>
from .student import Student
File "C:\me\myProject\myApp\Members\models\student.py", line 7, in <module>
from .profile import Profile
ImportError: cannot import name 'Profile'
我的想法是,这会创建一些django不能实现的环形链接,但我对此理解不足以解决该问题。
想法:Student表可以是它自己的表,但是感觉很浪费,因为Profile表已经具有必要的列
修改
我认为这是this post
的副本