1

我有一个带有这样模型的会计模块:

class Accounting_period(models.Model):
    start_date = models.DateField()
    end_date = models.DateField()
    base_pdf = models.FileField()

我有一个每天晚上午夜运行的管理功能,一旦 filter(end_date__lt=datetime.datetime.today()) 它通过 pisa 生成发票 PDF 并将该文件对象保存在 base_pdf 中。PDF 可通过网站上的链接获取,供会计人员使用。

但是,在 end_date 之后,会出现需要将完成的额外工作追溯到该发票的情况。然后需要创建一个新的“回溯”发票,如“Invoice-2.pdf”,它只有新的回溯总计(所以我们不会混淆会计人员)。我想将其保存到 Accounting_period 模型中,以便我可以轻松地在网页上显示链接。然而,我对向模型中添加一堆 FileFields 的想法感到畏缩。

我想知道是否有人对这个问题有很好的解决方案。我正在设想与 M2M 相似的东西,对于模板中的每个会计期间,我可以这样做:

templates.py
   <ul>
   {% for invoice in accounting_period_obj.invoices.all %}
      <li><a href="{{invoice.url}}">{{invoice.name}}</a>
   {% endfor %}
   </ul>

ManyToManyField “通过”参数看起来很有希望,但我没有链接到另一个模型。

4

1 回答 1

0

您可以为此使用 ForeignKey(一对多关系):

class AccountingPeriodBackDated(models.Model):
    accounting_period= models.ForeignKey(AccountingPeriod, related_name="backdates")
    pdf = models.FileField()

然后使用

templates.py
   <ul>
      <li><a href="{{accounting_period_obj.base_pdf.url}}">{{accounting_period_obj.base_pdf.name}}</a> (Original)</li>
      {% for o in accounting_period_obj.backdates.all %}
         <li><a href="{{o.pdf.url}}">{{o.pdf.name}}</a></li>
      {% endfor %}
   </ul>

有关更多信息,请参阅:https ://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey

于 2011-06-06T09:59:23.683 回答