ModelForm, ImageField and User

7 views
Skip to first unread message

l5x

unread,
Dec 23, 2007, 8:10:44 PM12/23/07
to Django users
class Photo(models.Model):
user = models.ForeignKey(User)
main = models.BooleanField(_('Is main?'))
icon = models.ImageField(_('Icon'), upload_to='photo', null=True,
blank=True,
help_text=_('Don\'t touch! It will be
generated automatically'))
medium = models.ImageField(_('Medium'), upload_to='photo',
null=True, blank=True,
help_text=_('Don\'t touch! It will be
generated automatically'))
big = models.ImageField(_('Photo'), upload_to='photo', null=True,
blank=True)

description = models.TextField(_('Description'))

class Admin:
list_display = ('user', 'description')

def _save_FIELD_file(self, field, filename, raw_contents,
save=False):
import sha
import random
import Image
ext = filename[-3:]
if ext == 'JPG' or ext == 'jpg':
ext = 'JPEG'
filename = sha.new(filename+str(random.random())).hexdigest()
filename = "%s_%s.%s" % (self.user_id, filename, ext)
super(Photo, self)._save_FIELD_file(field, filename,
raw_contents, save=False)
im = Image.open(u'/home/project/site_media/'+self.big)
im.thumbnail((300, 400), Image.ANTIALIAS)
im.save('/home/project/site_media/photo/'+filename, ext)

med = Image.open(u'/home/project/site_media/'+self.big)
med.thumbnail((120, 160), Image.ANTIALIAS)
filename = sha.new(str(random.random())).hexdigest()
filename = "%s_%s.%s" % (self.user_id, filename, ext)
med.save('/home/project/site_media/photo/'+filename, ext)
self.medium = 'photo/%s' % filename

icon = Image.open(u'/home/project/site_media/'+self.big)
icon.thumbnail((60, 80), Image.ANTIALIAS)
filename = sha.new(str(random.random())).hexdigest()
filename = "%s_%s.%s" % (self.user_id, filename, ext)
icon.save('/home/project/site_media/photo/'+filename, ext)
self.icon = 'photo/%s' % filename


class PhotoForm(ModelForm):
user = forms.IntegerField(widget=forms.HiddenInput)
class Meta:
model = Photo
fields = ('user', 'big', 'main', 'description')
==

def gallery(request):
data = request.POST.copy()
try:
a = data['main']
other = Photo.objects.filter(user=request.user)
for o in other:
o.main = False
print "main? ", o.main
o.save()

except KeyError:
print "key error"
photos = Photo.objects.filter(user=request.user).count()
print photos
if photos == 0:
data['main'] = 'on'


if request.POST:
photo_form = PhotoForm(request.POST, request.FILES)
if photo_form.is_valid():
photo_form.save()
else:
pre_data = {'user': request.user.id }
photo_form = PhotoForm(pre_data)

gallery = Photo.objects.filter(user=request.user)

return render_to_response('Member/gallery.html', {'gallery':
gallery, 'photo_form': photo_form},

context_instance=RequestContext(request))

I have two issues:

1) When I use photo_form.save() I get: (1048, "Column 'user_id' cannot
be null"), despite the fact that the user is in the POST data:
description: u'my desc'
user: u'2'
2) I wrote:

pre_data = {'user': request.user.id }
photo_form = PhotoForm(pre_data)

And there is a HiddenInput for User ForeignKey in the PhotoForm

But is there any other way to pass the information about User?

lispingng

unread,
Dec 24, 2007, 2:44:42 PM12/24/07
to Django users
i think you should look at using threadlocals to access users outside
the request instead of sending the user id in a hidden field.
http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser
quite straightforward

l5x

unread,
Dec 25, 2007, 9:20:34 AM12/25/07
to Django users
Thank you, I will check that. I wasn't aware of that possibility.

Muchanic

unread,
Dec 27, 2007, 2:22:10 AM12/27/07
to Django users
Just FYI and for anyone else who comes across this thread - I had the
same problem, trying to pass a ForeignKey via HiddenInput in an app
I'm working on. If you need to/want to do it this way, the trick is
that you have to use a ModelChoiceField and not an IntegerField for
Django to be able to do it's magic.

e.g. in my case I had something like

class CourseAddForm(forms.ModelForm):
institution = forms.ModelChoiceField(Institution.objects.all(),
widget=forms.HiddenInput())

l5x

unread,
Dec 28, 2007, 9:02:42 PM12/28/07
to Django users
You are right. When I load the page I can see the proper value in the
source - it's working. But when I submit the data I get: "(Hidden
field user) Select a valid choice. That choice is not one of the
available choices."

How can I fix it?

Best regards
Reply all
Reply to author
Forward
0 new messages