将用户模型扩展两次?

时间:2016-02-06 04:09:23

标签: python django

注意 - 我不确定是否允许此类问题,如果没有,我会道歉。

您好, 所以到现在为止我一直在使用Django-Allauth在我的网站上进行注册,但是我发现它非常有限,因此我决定开发自己的网站注册系统。在这个阶段,我希望有两种不同的注册表单,我的问题/关注点是我应该如何最好地扩展用户模型。我应该有两个具有一对一字段的模型,还是应该有两个不与用户模型连接的模型,只需在注册视图中放置两个表单(一个用于用户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

1 个答案:

答案 0 :(得分:0)

我建议更直接和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)

关于你的错误......我想这是因为你还没有完成迁移。