Django模型和查询集

时间:2018-10-30 09:39:25

标签: django django-models django-queryset

我想得到您的帮助,因为我在Django Web应用程序中被阻止。

我的模型中有两个类:

class Document(models.Model):
    code = models.CharField(max_length=25, verbose_name=_('code'), unique=True, null=False, blank=False, default='')
    title = models.CharField(max_length=512, verbose_name=_('document title'))

class Download(models.Model):
    email = models.CharField(max_length=150, verbose_name=_('e-mail'), null=False)
    country = models.ForeignKey(Country, verbose_name=_('country'), related_name='download')
    doc = models.ForeignKey(Document, verbose_name=_('Document'), null=True)
    token = models.CharField(max_length=40, verbose_name=_('download token'), unique=True, null=False)
    expiration_date = models.DateTimeField(verbose_name=_('expiration date'), null=False)

如您所见,由于DownloadDocument类被绑定到ForeignKey类。

现在,在我的views.py中,我有这个:

def form_valid(self, form):
    email = form.cleaned_data['email']
    country = form.cleaned_data['country']

    for checkbox in self.request.GET.getlist('DocumentChoice'):
        document = Document.objects.get(code=checkbox)
        token = self.gen_token(email)

        now = timezone.now()
        expiration = now + settings.DOWNLOAD_VALIDITY
        Download.objects.create(email=email, country=country, doc_id=checkbox, token=token, expiration_date=expiration)

    return super(HomeView, self).form_valid(form)

我想做什么:

在这一行:Download.objects.create(email=email, country=country, doc_id=checkbox, token=token, expiration_date=expiration)

我想设置document.code = checkbox,但我并没有克服寻找实现此目的的好方法。 checbox对应于document模型中的代码列表。

我尝试过doc_codedoc_id_code ...,但是找不到document.code类中引用download的良好语法。

谢谢

0 个答案:

没有答案
相关问题