How to use django model with foreign key ? Help plss

87 views Asked by At

Models.py

 class Member(models.Model):
        username = models.CharField(max_length=50, unique=True)
        password = models.CharField(max_length=50)
        document = models.ForeignKey("Document")

    class Document(models.Model):
        docfile = models.FileField(upload_to='documents/%Y/%m/%d')

document is foreign key

Vievs.py

def list(request):
    try:
        member = Member.objects.filter(username=request.user.username)[0]
        return render_to_response('list.html', locals())
    except Exception as e:
        print e

   # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document(docfile = request.FILES['docfile'])
            newdoc.save()

            # Redirect to the document list after POST
            return HttpResponseRedirect(reverse('kullanici.views.list'))
    else:
        form = DocumentForm() # A empty, unbound form


    return render_to_response(
        'list.html',
        {'form': form},
        context_instance=RequestContext(request)

member foreign key document with connected error ? Databases were created manually but it should change nothing.

1

There are 1 answers

0
malisit On BEST ANSWER

You may use django-registration-redux for authentication, after that you should use this code sample to indicate your foreign key:

from django.contrib.auth.models import User

class Document(models.Model):
    user = models.ForeignKey(User, null=True, blank=True)
    ...