我正在尝试重组数据库模型以包含比django.contrib.auth.models.User
多的数据。但是,对models.py
和views.py
进行更改,然后运行python manage.py makemigrations
并不能正确更新我的迁移。显然,Django仍然认为我正在使用代理模型。
我不得不更改users/migrations/0001_initial.py
以避免依赖错误,然后我运行python manage.py makemigrations
并得到了No changes detected
。
users/migrations/0001_initial.py
(更改前)
# Generated by Django 2.2 on 2019-05-01 03:13
from django.db import migrations
import users.managers
class Migration(migrations.Migration):
initial = True
dependencies = [
('auth', '0011_update_proxy_permissions'),
]
operations = [
migrations.CreateModel(
name='Person',
fields=[
],
options={
'proxy': True,
'constraints': [],
'indexes': [],
},
bases=('auth.user',),
managers=[
('objects', users.managers.PersonManager()),
],
),
]
users/migrations/0001_initial.py
(更改后)
# Generated by Django 2.2 on 2019-05-01 03:13
from django.db import migrations
import users.managers
class Migration(migrations.Migration):
initial = True
dependencies = []
operations = []
users/models.py
(更改前)
from django.db import models
from django.contrib.auth.models import User
from . import managers
class Person(User):
objects = managers.PersonManager()
class Meta:
proxy = True
users/models.py
(更改后)
from django.db import models
from django.contrib.auth.models import User
from users import managers
class Profile:
user = models.OneToOneField(User, on_delete=models.CASCADE)
phone = models.CharField(max_length=30)
objects = managers.ProfileManager()
users/views.py
from django.shortcuts import render, redirect
from django.contrib.auth.models import User
from users.models import Profile
def index(request):
profile = Profile.objects.get(user_pk=request.session['id']) \
if 'id' in request.session else None
return render(request, 'users/index.html', {
'profile': profile,
})
def login(request):
valid, response = Profile.objects.login_register(request, 'login')
if not valid:
for error in response:
messages.error(request, error)
return redirect('users:index')
profile = Profile.objects.get(pk=response)
messages.success(request, 'Welcome back, %s!' % profile.user.first_name)
request.session['id'] = response
return redirect('users:index')
def register(request):
valid, response = Profile.objects.login_register(request, 'register')
if not valid:
for error in response:
messages.error(request, error)
return redirect('users:index')
messages.success(request, 'You have successfully created an account.')
request.session['id'] = response
return redirect('users:index')
def logout(request):
del request.session['id']
messages.success(request, 'You have successfully logged out.')
return redirect('users:index')
当我尝试登录或注册时,我希望页面重新加载,并将登录/注册表单更改为用户的个人资料页面。
错误消息
Internal Server Error: /profile/
Traceback (most recent call last):
File "/home/matt/Repositories/pharmasseuse/env/lib/python3.5/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/matt/Repositories/pharmasseuse/env/lib/python3.5/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/matt/Repositories/pharmasseuse/env/lib/python3.5/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/matt/Repositories/pharmasseuse/users/views.py", line 12, in index
if 'id' in request.session else None
File "/home/matt/Repositories/pharmasseuse/env/lib/python3.5/site-packages/django/db/models/manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/matt/Repositories/pharmasseuse/env/lib/python3.5/site-packages/django/db/models/query.py", line 399, in get
clone = self.filter(*args, **kwargs)
File "/home/matt/Repositories/pharmasseuse/env/lib/python3.5/site-packages/django/db/models/query.py", line 892, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/home/matt/Repositories/pharmasseuse/env/lib/python3.5/site-packages/django/db/models/query.py", line 910, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/home/matt/Repositories/pharmasseuse/env/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1290, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/home/matt/Repositories/pharmasseuse/env/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1318, in _add_q
split_subq=split_subq, simple_col=simple_col,
File "/home/matt/Repositories/pharmasseuse/env/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1190, in build_filter
lookups, parts, reffed_expression = self.solve_lookup_type(arg)
File "/home/matt/Repositories/pharmasseuse/env/lib/python3.5/site-packages/django/db/models/sql/query.py", line 1049, in solve_lookup_type
_, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
File "/home/matt/Repositories/pharmasseuse/env/lib/python3.5/site-packages/django/db/models/sql/query.py", line 297, in get_meta
return self.model._meta
AttributeError: 'NoneType' object has no attribute '_meta'
感谢您的帮助。
答案 0 :(得分:1)
我认为更改迁移文件不是一个好主意。您可以直接在模型中进行更改,更改模型后,运行./manage.py makemigrations
来生成新的迁移文件。
此外,您需要从models.Model
继承模型类:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
phone = models.CharField(max_length=30)
objects = managers.ProfileManager()
(来自评论)随着您的经理更换,我认为您可以采用以下任一解决方案:
ProfileManager
和PersonManager
都保留在users.manager
中,然后运行makemigrations
。# Generated by Django 2.2 on 2019-05-01 03:13
from django.db import migrations
import users.managers
class Migration(migrations.Migration):
initial = True
dependencies = [
('auth', '0011_update_proxy_permissions'),
]
operations = [
migrations.CreateModel(
name='Person',
fields=[
],
options={
'proxy': True,
'constraints': [],
'indexes': [],
},
bases=('auth.user',),
# managers=[
# ('objects', users.managers.PersonManager()),
# ],
),
]