Hi, Am i doing this in the correct way? I am trying to accomplish a
UserProfile having multiple profile images. IE if a user uploads a
profile photo it gets saved as the default profile pic but if they
upload another pic then that becomes the default pic but i need to
keep records of all the profile pics that user has uploaded so that if
they view there profile pics they can see them all. Is this  1 to many
on UserProfile or a ManyToMany? as when i try a 1 to many i cant seem
to get the code working.

any help would be greatly appreciated.


code below.

MODELS.py
from django.contrib.auth.models import User

class UserProfile(models.Model):
    user = models.OneToOneField(User)

class UserProfilePic(models.Model):
    userprofile = models.ForeignKey(UserProfile)
    profilepic = models.ImageField(upload_to='photos')

FORMS.py
from .models import UserProfilePic
class UserProfilePicForm(ModelForm):
    class Meta:
        model = UserProfilePic
        fields = ('profilepic',)

VIEWS.py
def userprofilepic(request):
    c = {}
    c.update(csrf(request))

    u = User.objects.get(pk=1) #just testing with 1 user
    form = UserProfilePicForm(instance=u.get_profile())
    if request.method == "POST":
        form =
UserProfilePicForm(request.POST,request.FILES,instance=u.get_profile())
        if form.is_valid():
            form.save()
        else:
            return HttpResponse('failed')
    c['form'] = form
    return
render_to_response('photo.html',c,context_instance=RequestContext(request))

SQL OUTPUT thats been run on the view which doesnt seem to update
userprofilepic table

{'time': '0.001', 'sql': u'SELECT "auth_user"."id",
"auth_user"."username", "auth_user"."first_name",
"auth_user"."last_name", "auth_user"."email", "auth_user"."password",
"auth_user"."is_staff", "auth_user"."is_active",
"auth_user"."is_superuser", "auth_user"."last_login",
"auth_user"."date_joined" FROM "auth_user" WHERE "auth_user"."id" = 1
'}{'time': '0.000', 'sql': u'SELECT "mysite_userprofile"."id",
"mysite_userprofile"."user_id" FROM "mysite_userprofile" WHERE
"mysite_userprofile"."user_id" = 1 '}{'time': '0.000', 'sql': u'SELECT
(1) AS "a" FROM "mysite_userprofile" WHERE "mysite_userprofile"."id" =
1 LIMIT 1'}{'time': '0.001', 'sql': u'UPDATE "mysite_userprofile" SET
"user_id" = 1 WHERE "mysite_userprofile"."id" = 1 '}

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to