当我尝试通过管理面板将数据添加到Faculty
模型时,上传照片会给我一个错误:
/ admin / institute / faculty / add /
的TypeError 'Department'类型的对象没有len()
models.py:
MEDIA_ROOT_IMAGES = 'images/'
def __get_path_faculty__(instance, filename):
upload_dir = os.path.join(MEDIA_ROOT_IMAGES,instance.dept,'Faculty')
if not os.path.exists(upload_dir):
os.makedirs(upload_dir)
return os.path.join(upload_dir, filename)
class Department(models.Model):
dept_code = models.CharField(unique=True, max_length = 2, primary_key = True, blank = False)
.....
class Faculty(models.Model):
name = models.CharField(max_length = 50, blank = False)
dept = models.ForeignKey(Department, blank=False)
photo = models.ImageField(upload_to = __get_path_faculty__, default = 'null')
.....
faculty_id = models.CharField(unique=True, max_length =100, editable = False)
def save(self, *args, **kwargs):
if not self.faculty_id:
self.faculty_id = self.email_off.split('@')[0]
super(Faculty,self).save(*args,**kwargs)
views.py:
def faculty(request,dept_code):
dept_code1 = dept_code[:2].upper()
try:
dept = Department.objects.get(dept_code = dept_code1)
context_dict['Department'] = dept
except:
raise Http404
try:
faculties = Faculty.objects.filter(dept=dept_code1)
context_dict['Faculties'] = faculties
except:
raise Http404
return render(request,'faculty.html',context_dict)
回溯:
File "C:\Users\shiva\Anaconda\lib\site-packages\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\shiva\Anaconda\lib\site-packages\django\contrib\admin\options.py" in wrapper
616. return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Users\shiva\Anaconda\lib\site-packages\django\utils\decorators.py" in _wrapped_view
110. response = view_func(request, *args, **kwargs)
File "C:\Users\shiva\Anaconda\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
57. response = view_func(request, *args, **kwargs)
File "C:\Users\shiva\Anaconda\lib\site-packages\django\contrib\admin\sites.py" in inner
233. return view(request, *args, **kwargs)
File "C:\Users\shiva\Anaconda\lib\site-packages\django\contrib\admin\options.py" in add_view
1516. return self.changeform_view(request, None, form_url, extra_context)
File "C:\Users\shiva\Anaconda\lib\site-packages\django\utils\decorators.py" in _wrapper
34. return bound_func(*args, **kwargs)
File "C:\Users\shiva\Anaconda\lib\site-packages\django\utils\decorators.py" in _wrapped_view
110. response = view_func(request, *args, **kwargs)
File "C:\Users\shiva\Anaconda\lib\site-packages\django\utils\decorators.py" in bound_func
30. return func.__get__(self, type(self))(*args2, **kwargs2)
File "C:\Users\shiva\Anaconda\lib\site-packages\django\utils\decorators.py" in inner
145. return func(*args, **kwargs)
File "C:\Users\shiva\Anaconda\lib\site-packages\django\contrib\admin\options.py" in changeform_view
1467. self.save_model(request, new_object, form, not add)
File "C:\Users\shiva\Anaconda\lib\site-packages\django\contrib\admin\options.py" in save_model
1078. obj.save()
File "C:\Users\shiva\Documents\GitHub\iitbhu - Copy (3)\src\institute\models.py" in save
130. super(Faculty,self).save(*args,**kwargs)
File "C:\Users\shiva\Anaconda\lib\site-packages\django\db\models\base.py" in save
710. force_update=force_update, update_fields=update_fields)
File "C:\Users\shiva\Anaconda\lib\site-packages\django\db\models\base.py" in save_base
738. updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "C:\Users\shiva\Anaconda\lib\site-packages\django\db\models\base.py" in _save_table
822. result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "C:\Users\shiva\Anaconda\lib\site-packages\django\db\models\base.py" in _do_insert
861. using=using, raw=raw)
File "C:\Users\shiva\Anaconda\lib\site-packages\django\db\models\manager.py" in manager_method
127. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\shiva\Anaconda\lib\site-packages\django\db\models\query.py" in _insert
920. return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Users\shiva\Anaconda\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
970. for sql, params in self.as_sql():
File "C:\Users\shiva\Anaconda\lib\site-packages\django\db\models\sql\compiler.py" in as_sql
928. for obj in self.query.objs
File "C:\Users\shiva\Anaconda\lib\site-packages\django\db\models\fields\files.py" in pre_save
315. file.save(file.name, file, save=False)
File "C:\Users\shiva\Anaconda\lib\site-packages\django\db\models\fields\files.py" in save
90. name = self.field.generate_filename(self.instance, name)
File "C:\Users\shiva\Documents\GitHub\iitbhu - Copy (3)\src\institute\models.py" in __get_path_faculty__
17. upload_dir = os.path.join(MEDIA_ROOT_IMAGES,instance.dept,'Faculty')
File "C:\Users\shiva\Anaconda\lib\ntpath.py" in join
66. p_drive, p_path = splitdrive(p)
File "C:\Users\shiva\Anaconda\lib\ntpath.py" in splitdrive
114. if len(p) > 1:
Exception Type: TypeError at /admin/institute/faculty/add/
Exception Value: object of type 'Department' has no len()
答案 0 :(得分:1)
这是导致错误的行:
upload_dir = os.path.join(MEDIA_ROOT_IMAGES,instance.dept,'Faculty')
instance.dept
是Department
的一个实例。你可能想要的是部门代码。这可以通过instance.dept.dept_code
访问。
答案 1 :(得分:0)
您收到此错误,因为类len()
中未定义Department
函数。并且,您似乎正在.len()
类的实例上尝试Department
。
答案 2 :(得分:-1)
为了在类上使用len()方法,您需要在类中实现 len 方法。 更多信息:http://www.diveintopython.net/object_oriented_framework/special_class_methods2.html