2

Here i have two models:

  1. ProfilePic

  2. Member

ProfilePic's user variable extends from Member's username (this is so i can have one username in the DB for all other forms and models).

Now ProfilePic is used as a form, and in my views.py I want to add:

member_obj = Member.objects.get(pk=username)

to my ProfilePic form. However, when I run my code, it doesn't give an error but it doesn't render the information in the db either. So I'm confused as to whats going on here.

What am i doing wrong? Thanks in advance !

# models.py

class ProfilePic(models.Model):
    user = models.ForeignKey(Member, related_name='%(class)s_user', null=True)
    text = models.TextField(max_length=4096)
    thumbnail = models.FileField(upload_to='media', null=True)


class Member(models.Model):
    username = models.CharField(max_length=16, primary_key=True)
    password = models.CharField(max_length=16)
    profile = models.OneToOneField(Profile, null=True)
    following = models.ManyToManyField("self", symmetrical=True)


# forms.py

from django import forms
from .models import ProfilePic


class UploadFileForm(forms.ModelForm):
    class Meta:
        model = ProfilePic
        fields = ['text','thumbnail']


# views.py

def profile(request):
username = request.session['username']
member_obj = Member.objects.get(pk=username)
if request.POST:
    invitations = Invitation.objects.filter(to_user=username)
    form = UploadFileForm(request.POST,request.FILES, instance=member_obj)
    form.save()
    picture = ProfilePic.objects.all()
    return render(request, 'social/profile.html', {
        'appname': appname,
        'username': username,
        'invitations':invitations,
        'picture' : picture,
        'form' : form,
        'loggedin': True}
         )

3 Answers 3

1

You are passing a Member instance to a ProfilePic model form.

What you want to do is:

form = UploadFileForm(request.POST, request.FILES,
                      instance=member_obj.profile_pic_user)

So you get a ProfilePic instance.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your reply! Did you mean form = UploadFileForm(request.POST, request.FILES, instance=member_obj.ProfilePic.user) ? Because i dont have a variable called profile_pic_user
You have related_name='%(class)s_user'. Maybe it is member_obj.profilepic_user. I thought camel case were converted to snake case, but maybe it is just lower-cased.
Well, actually it's wrong. member_obj.profilepic_user is a related manager. You could use member_objs.profilepic_user.first() but what is the expected behavior if you have many ProfilePic instances per Member instance?
Well it will just output all the images on the screen i guess.. Also tried member_objs.profilepic_user.first() but get this error 'Member' object has no attribute 'member_obj'
1

View is just a function. You get a Member object from the database, assign it to a member_obj variable, but you are not actually doing anything with it. You want to assign it to a ProfilePic object. Also, I don't think this line picture = ProfilePic.objects.all() does what you intend to do. You are getting a list of all profile picture objects instead of just one.

3 Comments

Yes you are correct, that line was just me playing around with the code, once i get this username sorted i will be able to display just one image that the user has posted. You also mentioned that i need to assign it to a ProfilePic variable, how would i do that?
The way Aviah Laor suggested in his answer would work
Or you could do something like in this question stackoverflow.com/questions/936376/…
0

You have to add it to the saved object. You do that by telling the form to create the object, but not saving it to the DB yet.
Then, set the field, and save to the DB.

Add this lines to the view, instead of the form.save():

profile_pic = form.save(commit=False) #not saving to db
member_obj = Member.objects.get(username=request.user.username)
profile_pic.user = member_obj
profile_pic.save() # now it's saved

8 Comments

Thank you for your reply! i just tried this and i am getting this error: 'Member' object has no attribute 'member_obj'
hmm really strange, now i just get this error : username and nothing else.. It refers to this line member_obj = Member.objects.get(pk=form.cleaned_data['username']) in my code
what is the username you want to save?
the username i am logged in with, in my app you can register and login and the usernames and passwords are stored in the db
you should have used a foreign key of User to Member. Fixed the answer as is
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.