如何使用外键的django模型?帮助plss

时间:2015-06-07 11:31:56

标签: django foreign-keys django-views

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')

文件是外键

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)

成员外键文件有连接错误?数据库是手动创建的,但它不会改变任何内容。

1 个答案:

答案 0 :(得分:0)

您可以使用django-registration-redux进行身份验证,之后您应该使用此代码示例来指示您的外键:

from django.contrib.auth.models import User

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