alex
alex

Reputation: 141

Django ModelForm not displayed in template

I've got a few ModelForms at the bottom of my models.py file. Some of the modelforms are working (i.e. they are displayed in the template correctly).

These are 2 of the several that work:

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

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

Others don't work though. Even using the same view (passing the different ModelForm) to the same template. These are 2 that don't work:

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

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

Not really sure what I'm doing wrong... I've tried to run syncdb, but that didn't help. Also, it looks like the form objects are being created fine:

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

They're just not being displayed...Any thoughts?

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

FYI, sample view that works:

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

Doesn't Work:

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

Sample Template:

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

as requested:

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

Upvotes: 1

Views: 2284

Answers (1)

alex
alex

Reputation: 141

Thanks for your help everyone, esp. Ale. Tried to print the form in the shell and got a type error.

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.

Upvotes: 2

Related Questions