I've got a model with an ImageField in it:

image = models.ImageField(upload_to='images/wrestlers',
help_text='...')

I can upload images no problem using the Admin app.

I wish to upload images in my own Django app, so I created a form:

class WrestlerProfileForm(forms.ModelForm):
    class Meta:
        model = WrestlerProfile
        exclude = ('user',)

Now say I already saved a wrestler profile in admin and uploaded a
picture. In admin, if I re-edit that same profile, the image field is
still set (i.e. contains an image), and I can leave it as is, just
edit the other fields and leave the image field alone, it saves no
problem.

If I do this in my app, it won't populate the image field when the
form is first shown, even though I use instance=profile when I
initialize the form, it only populates the other fields. If I hit
save, it complains that the image field is required. I am able to save
if I upload another picture, but this is not what I want to have to
do, always upload a picture, just to be able to save the profile.

Here is my view:

@login_required
def edit_profile(request):
    try:
        profile =
WrestlerProfile.objects.get(user__username=request.user.username)
    except WrestlerProfile.DoesNotExist:
        profile = None
    if request.method == 'POST':
        form = WrestlerProfileForm(request.POST, request.FILES)
        if form.is_valid():
            wrestler = form.save(commit=False)
            wrestler.user = request.user
            if not profile is None:
                wrestler.id = profile.id
            wrestler.save()
            return HttpResponseRedirect(request.META['PATH_INFO'])
    else:
        if profile == None:
            form = WrestlerProfileForm()
        else:
            form = WrestlerProfileForm(instance=profile)
    return render_to_response('registration/profile.html', {
        'form': form,
    }, context_instance=RequestContext(request))

The reason why I save with commit=False then set the wrestler.id to
profile.id, is because if you look at my form, you may notice I
exclude the user field, which I actually get from the current logged
in user.

However, the problem is before that, it actually complains about the
missing image field at the time I execute form.is_valid().

Any ideas how to get around this issue?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
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