我有用户模型,用户可以是1或2。 根据要创建的用户类型,我想将配置文件与模型相关联。如果输入1,则为Person,输入2为公司。
我尝试过在models.py中编写代码,并遵循以下教程:https://simpleisbetterthancomplex.com/tutorial/2016/07/28/how-to-create-django-signals.html
signals / apps / entitiesmodels.py
class CompanyModel(AuditedModel):
name = models.CharField(max_length=64, db_index=True, verbose_name='Name', null=True, blank=True)
class PersonModel(AuditedModel):
name = models.CharField(max_length=64, db_index=True, verbose_name='Name', null=True, blank=True)
class Tester(PersonModel,PersistentModel):
# Link with user
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, blank=True, related_name='%(class)s_user')
class Company(CompanyModel,PersistentModel):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, blank=True, related_name='%(class)s_user')
signals / apps / entities / signals.py
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.conf import settings
from . import models as entities_models
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def user_create_profile(sender, instance, created, **kwargs):
if created:
if instance.user_type == 1:
entities_models.Tester.objects.create(user=instance)
elif instance.user_type == 2:
entities_models.Company.objects.create(user=instance)
else:
pass
signals / apps / entities / app.py
from django.apps import AppConfig
class EntitiesConfig(AppConfig):
name ='entities'
def ready(self):
import entities.signals
signals / apps / entities / api_v1 / views.py
from signals.apps.entities import models
from . import serializers
from signals.libs.views import APIViewSet
class PersonViewSet(APIViewSet):
queryset = models.Person.objects.all()
serializer_class = serializers.PersonSerializer
signals / apps / entities / api_v1 / urls.py
from rest_framework.routers import DefaultRouter
from signals.apps.entities.api_v1 import views
# Create a router and register our viewsets with it.
app_name='entities'
router = DefaultRouter()
router.register(r'persons', views.PersonViewSet, base_name="entities-persons")
urlpatterns = router.urls
settings.py
LOCAL_APPS = (
'signals.apps.authentication',
'signals.apps.entities.apps.EntitiesConfig',
)
运行服务器时,错误是:
File "/home/gonzalo/Playground/signals3/signals/signals/apps/entities/api_v1/urls.py", line 2, in <module>
from signals.apps.entities.api_v1 import views
File "/home/gonzalo/Playground/signals3/signals/signals/apps/entities/api_v1/views.py", line 1, in <module>
from signals.apps.entities import models
File "/home/gonzalo/Playground/signals3/signals/signals/apps/entities/models.py", line 47, in <module>
class Person(PersonModel):
File "/home/gonzalo/.virtualenvs/signals-test/lib/python3.6/site-packages/django/db/models/base.py", line 108, in __new__
"INSTALLED_APPS." % (module, name)
untimeError: Model class signals.apps.entities.models.Person doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS
如果有人想检查一下,我在github上有示例代码:https://github.com/gonzaloamadio/django-signals3
答案 0 :(得分:0)
感谢您的回答,当使用这种在设置上引用应用程序的方式时,则应使用如下所示的导入方式:
signals / apps / entities / api_v1 / views.py
from signals.apps.entities import models
和urls.py
from entities.api_v1 import views