Exception Type: ValueError at /admin/delegation/studytask/add/
Exception Value: 'account.UserProfile' has no ForeignKey to 'delegation.StudyTask'.
不确定为什么会这样。思考?谢谢!
型号:
from django.db import models
from account.models import UserProfile
from studies.models import Study
# Create your models here.
class StudyTask(models.Model):
title = models.CharField(max_length=150)
description = models.TextField()
class DelegationLog(models.Model):
study = models.ForeignKey(Study)
del_log = models.FileField(upload_to='uploads/delegation_logs/%Y/%m/%d', null=True, blank=True)
staff = models.ManyToManyField(UserProfile, through='DelegatedStaff')
class DelegatedStaff(models.Model):
staff = models.ForeignKey(UserProfile)
del_log = models.ForeignKey(DelegationLog)
start_date = models.DateField('Start Date')
end_date = models.DateField('End Date')
approved_date = models.DateField('Date Approved by PI')
tasks = models.ManyToManyField(StudyTask)
def __str__(self):
return self.staff
管理:
from django.contrib import admin
from .models import DelegationLog, DelegatedStaff, StudyTask
# Register your models here.
class DelegatedStaffAdminInline(admin.TabularInline):
model = DelegatedStaff
class DelegationLogAdmin(admin.ModelAdmin):
list_display = ['study']
inlines = [DelegatedStaffAdminInline]
admin.site.register(StudyTask)
admin.site.register(DelegationLog, DelegationLogAdmin)
用户admin:
class ProfileInline(admin.TabularInline):
model = UserProfile
UserAdmin.inlines += [ProfileInline]
更新:这是追溯
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/delegation/studytask/add/
Django Version: 1.8.7
Python Version: 3.5.0
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'studies',
'account',
'delegation')
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',
'django.middleware.security.SecurityMiddleware')
Traceback:
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/contrib/admin/options.py" in wrapper
618. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapped_view
110. response = view_func(request, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
57. response = view_func(request, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/contrib/admin/sites.py" in inner
233. return view(request, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/contrib/admin/options.py" in add_view
1518. return self.changeform_view(request, None, form_url, extra_context)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapper
34. return bound_func(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapped_view
110. response = view_func(request, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/utils/decorators.py" in bound_func
30. return func.__get__(self, type(self))(*args2, **kwargs2)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/contextlib.py" in inner
30. return func(*args, **kwds)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/contrib/admin/options.py" in changeform_view
1482. formsets, inline_instances = self._create_formsets(request, self.model(), change=False)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/contrib/admin/options.py" in _create_formsets
1798. for FormSet, inline in self.get_formsets_with_inlines(*get_formsets_args):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/contrib/admin/options.py" in get_formsets_with_inlines
796. yield inline.get_formset(request, obj), inline
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/contrib/admin/options.py" in get_formset
1879. fields = flatten_fieldsets(self.get_fieldsets(request, obj))
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/contrib/admin/options.py" in get_fieldsets
354. return [(None, {'fields': self.get_fields(request, obj)})]
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/contrib/admin/options.py" in get_fields
1955. form = self.get_formset(request, obj, fields=None).form
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/contrib/admin/options.py" in get_formset
1950. return inlineformset_factory(self.parent_model, self.model, **defaults)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/forms/models.py" in inlineformset_factory
1016. fk = _get_foreign_key(parent_model, model, fk_name=fk_name)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/forms/models.py" in _get_foreign_key
988. parent_model._meta.object_name,
Exception Type: ValueError at /admin/delegation/studytask/add/
Exception Value: 'account.UserProfile' has no ForeignKey to 'delegation.StudyTask'.