#models.py
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL)
birthday = models.DateField(blank=True, null=True)
我将django.contrib.auth.views.models.User扩展为其他信息,例如生日等。
问题是我无法在birthday
中使用request.user.profile
访问views.py
。
我不得不使用额外的小部件来使应用程序更直观。
我是这样做的:
#forms.py
class ProfileEditForm(forms.ModelForm):
birthday = forms.DateField(widget=extras.SelectDateWidget)
class Meta:
model = Profile
fields = ('photo', 'phone_num', 'nationality', 'gender')
#views.py
@login_required
def edit(request):
if request.method == 'POST':
user_form = UserEditForm(instance = request.user,
data = request.POST)
profile_form = ProfileEditForm(instance = request.user.profile,
data = request.POST,
files = request.FILES)
if user_form.is_valid() and profile_form.is_valid():
request.user.profile.birthday = user_form.cleaned_data['birthday']
user_form.save()
profile_form.save()
else:
user_form = UserEditForm(instance=request.user)
profile_form = ProfileEditForm(
instance = request.user.profile
)
return render(request,
'views/edit_profile.html',
{'user_form' : user_form,
'profile_form' : profile_form})
完整的追溯:
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7fac3cbfa488>
Traceback (most recent call last):
File "/home/peterkim/PycharmProjects/brave/bravepeach_web/.venv/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/home/peterkim/PycharmProjects/brave/bravepeach_web/.venv/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
self.check(display_num_errors=True)
File "/home/peterkim/PycharmProjects/brave/bravepeach_web/.venv/lib/python3.5/site-packages/django/core/management/base.py", line 374, in check
include_deployment_checks=include_deployment_checks,
File "/home/peterkim/PycharmProjects/brave/bravepeach_web/.venv/lib/python3.5/site-packages/django/core/management/base.py", line 361, in _run_checks
return checks.run_checks(**kwargs)
File "/home/peterkim/PycharmProjects/brave/bravepeach_web/.venv/lib/python3.5/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/home/peterkim/PycharmProjects/brave/bravepeach_web/.venv/lib/python3.5/site-packages/django/core/checks/urls.py", line 14, in check_url_config
return check_resolver(resolver)
File "/home/peterkim/PycharmProjects/brave/bravepeach_web/.venv/lib/python3.5/site-packages/django/core/checks/urls.py", line 24, in check_resolver
for pattern in resolver.url_patterns:
File "/home/peterkim/PycharmProjects/brave/bravepeach_web/.venv/lib/python3.5/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/peterkim/PycharmProjects/brave/bravepeach_web/.venv/lib/python3.5/site-packages/django/urls/resolvers.py", line 313, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/home/peterkim/PycharmProjects/brave/bravepeach_web/.venv/lib/python3.5/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/peterkim/PycharmProjects/brave/bravepeach_web/.venv/lib/python3.5/site-packages/django/urls/resolvers.py", line 306, in urlconf_module
return import_module(self.urlconf_name)
File "/home/peterkim/PycharmProjects/brave/bravepeach_web/.venv/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/peterkim/PycharmProjects/brave/bravepeach_web/bravepeach/urls.py", line 23, in <module>
url(r'', include('webapp.urls')),
File "/home/peterkim/PycharmProjects/brave/bravepeach_web/.venv/lib/python3.5/site-packages/django/conf/urls/__init__.py", line 50, in include
urlconf_module = import_module(urlconf_module)
File "/home/peterkim/PycharmProjects/brave/bravepeach_web/.venv/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/peterkim/PycharmProjects/brave/bravepeach_web/webapp/urls.py", line 2, in <module>
from . import views
File "/home/peterkim/PycharmProjects/brave/bravepeach_web/webapp/views.py", line 4, in <module>
from .forms import UserRegistrationForm, UserEditForm, ProfileEditForm
File "/home/peterkim/PycharmProjects/brave/bravepeach_web/webapp/forms.py", line 37, in <module>
class UserEditForm(forms.ModelForm):
File "/home/peterkim/PycharmProjects/brave/bravepeach_web/.venv/lib/python3.5/site-packages/django/forms/models.py", line 257, in __new__
raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (birthday) specified for User
答案 0 :(得分:0)
正如@Alasdair所提到的,您需要更正UserEditForm类。回到自定义小部件,你可以做
class ProfileEditForm(forms.ModelForm):
class Meta:
model = Profile
fields = ('photo', 'phone_num', 'nationality', 'gender', 'birthday')
widgets = {
'birthday': extras.SelectDateWidget,
}