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?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to