2010-09-18 107 views
5

嘿,新的Django和需要幫助,當我將我的模型添加到Django的管理界面它看起來很好,但是當我嘗試添加或刪除數據庫中的條目我得到:Django中的類型錯誤與python 2.7

TypeError at /admin/Users/user/add/  
    coercing to Unicode: need string or buffer, tuple found 

我做了谷歌搜索,並補充說:

def __str__(self): 
    return "" 

爲了我的用戶模型類的末尾,但沒有成功。不確定是否必須在我的admin.py中輸入內容?我的User類中也沒有「add」方法,除了上面的方法外,它也不會返回任何其他內容。

感謝您的幫助!

用戶類:

class User(models.Model): 
GENDER_CHOICES = (
     ('M', 'Male'), 
     ('F', 'Female'), 
    ) 

username = models.CharField(max_length=30) 
email = models.EmailField() 
password = models.CharField(max_length=30) 

birth_date = models.DateField() 
description = models.CharField(max_length=200) 
gender = models.CharField(max_length = 1, choices = GENDER_CHOICES, default = "M") 
image = models.ImageField(upload_to="media/photos/") 
signupIP = models.IPAddressField() 
privateOrPublic = models.BooleanField(default=1) 

def __str__(self): 
    return "" 

而且在/用戶簡單admin.py/

from Users.models import User 
from django.contrib import admin 

admin.site.register(User) 

回溯:

Environment: 

Request Method: POST 
Request URL: http://127.0.0.1/admin/Users/user/add/ 
Django Version: 1.2.3 
Python Version: 2.7.0 
Installed Applications: 
['django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.sites', 
    'django.contrib.messages', 
    'django.contrib.admin', 
    'Users'] 
Installed Middleware: 
('django.middleware.common.CommonMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware') 


Traceback: 
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response 
    100.      response = callback(request, *callback_args, **callback_kwargs) 
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in wrapper 
    239.     return self.admin_site.admin_view(view)(*args, **kwargs) 
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view 
    76.      response = view_func(request, *args, **kwargs) 
File "C:\Python27\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func 
    69.   response = view_func(request, *args, **kwargs) 
File "C:\Python27\lib\site-packages\django\contrib\admin\sites.py" in inner 
    190.    return view(request, *args, **kwargs) 
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapper 
    21.    return decorator(bound_func)(*args, **kwargs) 
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view 
    76.      response = view_func(request, *args, **kwargs) 
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in bound_func 
    17.     return func(self, *args2, **kwargs2) 
File "C:\Python27\lib\site-packages\django\db\transaction.py" in _commit_on_success 
    299.      res = func(*args, **kw) 
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in add_view 
    795.     self.save_model(request, new_object, form, change=False) 
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in save_model 
    597.   obj.save() 
File "C:\Python27\lib\site-packages\django\db\models\base.py" in save 
    434.   self.save_base(using=using, force_insert=force_insert, force_update=force_update) 
File "C:\Python27\lib\site-packages\django\db\models\base.py" in save_base 
    517.       for f in meta.local_fields if not isinstance(f, AutoField)] 
File "C:\Python27\lib\site-packages\django\db\models\fields\files.py" in pre_save 
    255.    file.save(file.name, file, save=False) 
File "C:\Python27\lib\site-packages\django\db\models\fields\files.py" in save 
    91.   name = self.field.generate_filename(self.instance, name) 
File "C:\Python27\lib\site-packages\django\db\models\fields\files.py" in generate_filename 
    282.   return os.path.join(self.get_directory_name(), self.get_filename(filename)) 
File "C:\Python27\lib\site-packages\django\db\models\fields\files.py" in get_filename 
    279.   return os.path.normpath(self.storage.get_valid_name(os.path.basename(filename))) 
File "C:\Python27\lib\site-packages\django\utils\functional.py" in __getattr__ 
    276.    self._setup() 
File "C:\Python27\lib\site-packages\django\core\files\storage.py" in _setup 
    242.   self._wrapped = get_storage_class()() 
File "C:\Python27\lib\site-packages\django\core\files\storage.py" in __init__ 
    133.   self.location = os.path.abspath(location) 
File "C:\Python27\lib\ntpath.py" in abspath 
    465.     path = _getfullpathname(path) 

Exception Type: TypeError at /admin/Users/user/add/ 
Exception Value: coercing to Unicode: need string or buffer, tuple found 
+0

你能告訴我們更多你的代碼嗎?你有沒有自定義用戶類? – 2010-09-18 19:30:17

+0

我對管理網站並不熟悉,但如果您將\ __ str__更改爲\ __ unicode__仍然是一樣的? – juanefren 2010-09-18 19:31:23

+0

仍然一樣,認爲它與管理界面有關,而不是我的代碼,不確定是否。併發布更多代碼Ned。 – pyBite42 2010-09-18 19:53:13

回答

1

在你MEDIA_ROOT定義,更改您要更換有一個原始的字符串,否則你會取代一個字面單反斜槓而不是你的意思。

MEDIA_ROOT = os.path.join(os.path.dirname(file), "media").replace(r"\\", "//")