Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError in Django with python 2.7

Tags:

python

django

Hey, new to Django and needing assistance, when I add my model to the admin interface in Django it appeares fine, but when I try to add or delete an entry in the database I get:

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

I done a google search and added:

def __str__(self):
    return ""

To the end of my User model class but with no success. Not sure if I have to enter something into my admin.py? I also have no "add" method in my User class, it also returns nothing else other than the method above.

Thanks for any help!

The User Class:

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 ""

And the simple admin.py in /Users/

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

 admin.site.register(User)

Traceback:

 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
like image 875
pyBite42 Avatar asked Nov 15 '22 07:11

pyBite42


1 Answers

In your MEDIA_ROOT definition, change your replace to have a raw string, as otherwise you'll be replacing a literal single backslash rather than the two you meant.

MEDIA_ROOT = os.path.join(os.path.dirname(file), "media").replace(r"\\", "//")
like image 179
Daenyth Avatar answered Nov 17 '22 05:11

Daenyth