Saturday, June 15, 2013

Importing the user.id in form.

Hi,

I defined a model which has a 'ForeignKey' to the 'User' .

models.py :

class Design(models.Model):
title = models.CharField(max_length=100)
designer=models.ForeignKey(User)
description = models.TextField(null = True)
def __unicode__(self):
return self.title

forms.py :

class UploadForm(forms.ModelForm):  
    class Meta:
        model = Design
        fields = ('title','description')

views.py :

def me(request):
    if request.POST:
        form = UploadForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/home/')
        else:
            return render_to_response('home/invalid.html')
    else:
        form = UploadForm()
        args = {}
        args.update(csrf(request))
        args['form'] = form
        return render_to_response('home/upload.html', args,context_instance=RequestContext(request))

Now when I save the save the form, the 'designer=models.ForeignKey(User)' raises an error "Column 'designer_id' cannot be null"  .
A user after logging in can upload a new design.
How to store the " designer_id " i.e, the user.id  of the user who uploaded the design?

Can someone help me!!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate