Getting the following error while trying to create(&save) a django model with file field :
TypeError at /admin/app_name/template/add/
coercing to Unicode: need string or buffer, int found
Request Method: POST
Request URL: http://localhost:8000/admin/app_name/template/add/
Django Version: 1.4.3
Exception Type: TypeError
Exception Value: coercing to Unicode: need string or buffer, int found
Exception Location: path_to_python\python\lib\site-packages\django\utils\encoding.py
in force_unicode, line 71
here is models.py :-
class Template(models.Model):
title = models.CharField(max_length=300, unique=True)
template = models.FileField(upload_to='templates')
def __unicode__(self):
return self.title
class TemplateAdmin(admin.ModelAdmin):
def upload_file(request,*args, **kwargs):
if request.method == 'POST':
instance = Template(template=request.FILES['template'])
instance.title =request.POST['title']
instance.save()
admin.site.register(Template, TemplateAdmin)
here is settings.py:-
MEDIA_ROOT = os.path.join(os.path.dirname(__file__), "media")
Also, though there is this error when trying to save(create) a template object, still the file seems to be uploaded in the specified directory (as I can find it in that directory)... However no object instance is created (as Template.objects.all()
returns an empty list).