1

我的 models.py 文件底部有一些 ModelForms。一些模型表单正在工作(即它们在模板中正确显示)。

这些是其中的两个:

class Account_Balance_Form(ModelForm):
    class Meta:
        model = Account_Balance
        fields = ('date','balance')

class Asset_Form(ModelForm):
    class Meta:
        model = Asset
        exclude = ('account','id')

其他的虽然不行。即使使用相同的视图(传递不同的 ModelForm)到相同的模板。这些是 2 不起作用:

class Allocation_Form(ModelForm):
    class Meta:
        model = Allocation

class Deduction_Form(ModelForm):
    class Meta:
        model = Deduction

不太确定我做错了什么......我试过运行syncdb,但这没有帮助。此外,看起来表单对象的创建效果很好:

allocation_form <forecast.models.Allocation_Form object at 0x90f15ac>

他们只是没有被展示......有什么想法吗?

====================

仅供参考,有效的示例视图:

def view_allocation(request):
    form = Asset_Form()
    return render_to_response('alloc.html',
                                {'form': form})

不工作:

def view_allocation(request):
    form = Allocation_Form()
    return render_to_response('alloc.html',
                                {'form': form})

样本模板:

<html>
<body>
{{ form.as_p }}
</body>
</html>

按照要求:

class Allocation(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField(max_length=40)
    account = models.ForeignKey(Account)
    amount = models.DecimalField(max_digits=20,decimal_places=2)
    percent = models.DecimalField(max_digits=10,decimal_places=10)
    allocation_group = models.IntegerField(max_length=11)
    def __unicode__(self):
        return self.name


class Deduction(models.Model):
    iei = models.ForeignKey(Inc_Exp_Item, null=True, blank=True)
    name = models.CharField(max_length=40)
    amount = models.DecimalField(max_digits=20,decimal_places=2)
    percent = models.DecimalField(max_digits=10,decimal_places=10)
    before_tax = models.BooleanField()
    credit_debit = models.CharField(max_length=6, 
                                    choices=(('Debit','Income'),
                                             ('Credit','Expense'),))
    tax_category = models.ForeignKey(Tax_Category)
    account = models.ForeignKey(Account)
    active = models.BooleanField()
    deduct_taxes = models.BooleanField()
4

1 回答 1

2

感谢大家的帮助,尤其是。啤酒。试图在 shell 中打印表单并得到类型错误。

The object was being created, but couldn't be printed (or .as_p() ).

The problem was in the Account model that Allocation and Deduction had a foreign key to:

class Account(models.Model):
    user = models.ForeignKey(User)
    account_type = models.ForeignKey(Account_Type)
    def __unicode__(self):
        return self.account_type

I removed the unicode method and it worked. I guess unicode doesn't return another model's unicode method, haha.

于 2012-05-17T14:38:34.037 回答