Confused about uploading files (Cannot force both insert and updating in model saving)

1,344 views
Skip to first unread message

Roy Smith

unread,
Jun 5, 2011, 10:18:24 PM6/5/11
to Django users
I'm trying to figure out how to upload a file. I've got a model that
looks like:

class DataSet(models.Model):
file = models.FileField(upload_to='data/%Y/%m/%d')

and my view method is:

def create_data_set(request):
if request.method == 'POST':
form = DataSetForm(request.POST,
request.FILES)
if form.is_valid():
f = request.FILES['file']
data_set = DataSet()
data_set.save('foo', f)
return HttpResponseRedirect("/")
print "invalid"
else:
form = DataSetForm()
ctx = {'form': form}
ctx.update(csrf(request))
return render_to_response('chart/data_set.html', ctx)

with a form....

class DataSetForm(forms.Form):
file = forms.FileField()

When I execute this, I get a form containing a file picker (so far, so
good), but when I submit the form I get a mysterious error (below).
What am I doing wrong?


Environment:


Request Method: POST
Request URL: http://localhost:8000/dataset/create

Django Version: 1.3
Python Version: 2.6.1
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'chart']
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 "/Users/roy/lib/webchart/lib/python2.6/site-packages/Django-1.3-
py2.6.egg/django/core/handlers/base.py" in get_response
111. response = callback(request,
*callback_args, **callback_kwargs)
File "/Users/roy/WebChart/chart/views.py" in create_data_set
27. data_set.save('foo', f, False)
File "/Users/roy/lib/webchart/lib/python2.6/site-packages/Django-1.3-
py2.6.egg/django/db/models/base.py" in save
459. raise ValueError("Cannot force both insert and
updating in model saving.")

Exception Type: ValueError at /dataset/create
Exception Value: Cannot force both insert and updating in model
saving.

Kenneth Gonsalves

unread,
Jun 5, 2011, 10:23:20 PM6/5/11
to django...@googlegroups.com
On Sun, 2011-06-05 at 19:18 -0700, Roy Smith wrote:
> ctx = {'form': form}
> ctx.update(csrf(request))

indent these two lines
--
regards
KG
http://lawgon.livejournal.com
Coimbatore LUG rox
http://ilugcbe.techstud.org/

Michal Petrucha

unread,
Jun 6, 2011, 3:02:43 AM6/6/11
to django...@googlegroups.com
On Sun, Jun 05, 2011 at 07:18:24PM -0700, Roy Smith wrote:
> I'm trying to figure out how to upload a file. I've got a model that
> looks like:
>
> class DataSet(models.Model):
> file = models.FileField(upload_to='data/%Y/%m/%d')
>
> and my view method is:
>
> def create_data_set(request):
> if request.method == 'POST':
> form = DataSetForm(request.POST,
> request.FILES)
> if form.is_valid():
> f = request.FILES['file']
> data_set = DataSet()
> data_set.save('foo', f)

I suspect this is the problem: you create an empty instnce of DataSet
and then call its save method. model.save has three optional arguments
[1], in this case you call it with force_insert='foo', force_update=f.

Both f and 'foo' evaluate to True, which is an obvious conflict.

Michal

[1] https://docs.djangoproject.com/en/1.3/ref/models/instances/#django.db.models.Model.save

signature.asc

Roy Smith

unread,
Jun 6, 2011, 8:59:10 AM6/6/11
to Django users

> >    if form.is_valid():
> >             f = request.FILES['file']
> >             data_set = DataSet()
> >             data_set.save('foo', f)
>
> I suspect this is the problem: you create an empty instnce of DataSet
> and then call its save method. model.save has three optional arguments
> [1], in this case you call it with force_insert='foo', force_update=f.
>
> Both f and 'foo' evaluate to True, which is an obvious conflict.

Ah, yes, of course. I was mixing up model.save() and
model.FileField.save(). Thanks for setting me straight. What I
wanted was this:

if form.is_valid():
f = request.FILES['file']
data_set = DataSet(file=f)
data_set.save()

Reply all
Reply to author
Forward
0 new messages