python - 扩展用户模型两次?

标签 python django

注意 - 我不确定是否允许此类问题,如果不允许,我深表歉意。

您好, 所以到目前为止,我一直在使用 Django-Allauth 在我的网站上进行注册,但我发现它非常有限,因此我决定为网站开发自己的注册系统。在这个阶段我想要两种不同的注册表单,我的问题/担忧是我应该如何最好地扩展用户模型。我应该有两个模型都具有一对一的字段,还是应该有两个模型不与用户模型连接并且只在注册 View 中放置两个表单(一个用于用户 mnodel 和自定义字段)或也许是一种完全不同的方法。下面是我一直在玩弄的代码,但我将不胜感激一些关于我应该做什么的建议!干杯

模型 -

class StudentForm(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    test = models.CharField(max_length=10)

class EmployerForm(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    tester = models.CharField(max_length=20)

管理员 -

from django.contrib import admin
from models import StudentForm, EmployerForm

class StudentFormAdmin(admin.ModelAdmin):
    list_display = ["__unicode__"]

    class Meta:
        model = StudentForm

class EmployerFormAdmin(admin.ModelAdmin):
    list_display = ["__unicode__", "tester"]

    class Meta:
        model = StudentForm


admin.site.register(StudentForm, StudentFormAdmin)
admin.site.register(EmployerForm, EmployerFormAdmin)

错误-

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/admin/registration/studentform/

Django Version: 1.9.1
Python Version: 2.7.10
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sites',
 'main',
 'listings',
 'profiles',
 'allauth',
 'allauth.account',
 'allauth.socialaccount',
 'registration')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')



Traceback:

File "C:\Python27\Lib\site-packages\django\core\handlers\base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "C:\Python27\Lib\site-packages\django\core\handlers\base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Python27\Lib\site-packages\django\contrib\admin\options.py" in wrapper
  541.                 return self.admin_site.admin_view(view)(*args, **kwargs)

File "C:\Python27\Lib\site-packages\django\utils\decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "C:\Python27\Lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)

File "C:\Python27\Lib\site-packages\django\contrib\admin\sites.py" in inner
  244.             return view(request, *args, **kwargs)

File "C:\Python27\Lib\site-packages\django\utils\decorators.py" in _wrapper
  67.             return bound_func(*args, **kwargs)

File "C:\Python27\Lib\site-packages\django\utils\decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "C:\Python27\Lib\site-packages\django\utils\decorators.py" in bound_func
  63.                 return func.__get__(self, type(self))(*args2, **kwargs2)

File "C:\Python27\Lib\site-packages\django\contrib\admin\options.py" in changelist_view
  1468.                 self.list_max_show_all, self.list_editable, self)

File "C:\Python27\Lib\site-packages\django\contrib\admin\views\main.py" in __init__
  79.         self.get_results(request)

File "C:\Python27\Lib\site-packages\django\contrib\admin\views\main.py" in get_results
  174.         result_count = paginator.count

File "C:\Python27\Lib\site-packages\django\core\paginator.py" in _get_count
  72.                 self._count = self.object_list.count()

File "C:\Python27\Lib\site-packages\django\db\models\query.py" in count
  371.         return self.query.get_count(using=self.db)

File "C:\Python27\Lib\site-packages\django\db\models\sql\query.py" in get_count
  483.         number = obj.get_aggregation(using, ['__count'])['__count']

File "C:\Python27\Lib\site-packages\django\db\models\sql\query.py" in get_aggregation
  464.         result = compiler.execute_sql(SINGLE)

File "C:\Python27\Lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
  848.             cursor.execute(sql, params)

File "C:\Python27\Lib\site-packages\django\db\backends\utils.py" in execute
  79.             return super(CursorDebugWrapper, self).execute(sql, params)

File "C:\Python27\Lib\site-packages\django\db\backends\utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

File "C:\Python27\Lib\site-packages\django\db\utils.py" in __exit__
  95.                 six.reraise(dj_exc_type, dj_exc_value, traceback)

File "C:\Python27\Lib\site-packages\django\db\backends\utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

File "C:\Python27\Lib\site-packages\django\db\backends\sqlite3\base.py" in execute
  323.         return Database.Cursor.execute(self, query, params)

Exception Type: OperationalError at /admin/registration/studentform/
Exception Value: no such table: registration_studentform

最佳答案

我建议更直截了当 django扩展用户模型的方式:

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

USER_ROLE_STUDENT = 1
USER_ROLE_EMPLOYER = 2

USER_ROLES = (
        (USER_ROLE_STUDENT, 'Student'),
        (USER_ROLE_EMPLOYER, 'Employer'),
    )


class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name='user_profile')
    role = models.PositiveIntegerField(choices=USER_ROLES, default=USER_ROLE_STUDENT)
    test = models.CharField(max_length=10)


def create_user_profile(sender, instance, created, **kwargs):
    profile, created = UserProfile.objects.get_or_create(user=instance)
    if created:
        profile.save()

post_save.connect(create_user_profile, sender=User)

关于您的错误...我想这是因为您确实没有运行迁移。

关于python - 扩展用户模型两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35237221/

相关文章:

python - 颜色图不随 imshow() 改变

仅对正数求和的Python程序

python - 如何从 Python 3 中的 UTF-16 代码点获取字符?

django - 您的数据库 django 没有南数据库模块 'south.db.postgresql_psycopg2'

python - Django 4.04 中具有区域设置感知功能的正确工作千位分隔符

python - pretty-print PyParsing 树

python - Ffmpeg 映射和 filter_complex 子进程 - Python

python - Django 基于类的 View 中的类变量与方法

python - Django Celery 任务运行两次

django - Meta.fields 包含未在此 FilterSet : **** 上定义的字段