ModelForm excluded one field but it excludes another field

28 views
Skip to first unread message

Rohit Banga

unread,
Nov 13, 2012, 2:47:54 AM11/13/12
to django...@googlegroups.com
Hi All

I have the following models:

class Model1(models.Model):
     field1 = ...
     field2 = ...
     class Meta:
          abstract = True

class Model2(models.Model):
     field3 = ...
     field4 = ...
     class Meta:
          abstract = True

I have concrete implementations of the above:

class ConcreteModel1(Model1):
     field1_group = ... # foreign key to ConcreteModel3


class ConcreteModel2(Model2):
     field3_test = ... # foreign key to ConcreteModel4

I have abstract ModelForms:

 class Model1Form(ModelForm):
         class Meta:
          widgets = { ... }
          abstract = True

 class Model2Form(ModelForm):
         class Meta:
          widgets = { ... }
          abstract = True

I have concrete ModelForms:

 class ConcreteModel1Form(Model1Form):
         class Meta(Model1Form.Meta):
             model = ConcreteModel1
             exclude = ('field1_group')
             

 class ConcreteModel2Form(Model2Form):
         class Meta(Model2Form.Meta):
             model = ConcreteModel2
             exclude = ('field3_test')


Now as you can see there are parallels between the two model forms.
The problem is that in my template field1 is visible for ConcreteModel1Form (expected behavior)
But in another template field3 is not visible for ConcreteModel2Form (unexpected behavior).

If I comment out exclude=('field3_test') then field3 is visible in the template (expected behavior) but form validation fails since field3_test is None and is a required field.

What am I missing? This seems to be a problem with my code that i can't figure out. This behavior is clearly inconsistent and I am not able to see the difference in my code.

Any ideas how to debug this? What could be the problem?


Thanks
Rohit Banga
http://iamrohitbanga.com/

Sandeep kaur

unread,
Nov 17, 2012, 2:58:37 AM11/17/12
to django...@googlegroups.com
On Tue, Nov 13, 2012 at 1:17 PM, Rohit Banga <iamroh...@gmail.com> wrote:
> Hi All
>
<snip>
> model = ConcreteModel2
> exclude = ('field3_test')
>
>
> Now as you can see there are parallels between the two model forms.
> The problem is that in my template field1 is visible for ConcreteModel1Form
> (expected behavior)

> But in another template field3 is not visible for ConcreteModel2Form
> (unexpected behavior).
>
> If I comment out exclude=('field3_test') then field3 is visible in the
> template (expected behavior) but form validation fails since field3_test is
> None and is a required field.
>
> What am I missing? This seems to be a problem with my code that i can't
> figure out. This behavior is clearly inconsistent and I am not able to see
> the difference in my code.
>
> Any ideas how to debug this? What could be the problem?
>
When we exclude a field from a form, that field become invisible. But
as we know, that it is mandatory to fill the foreign key value (else
the form will become invalid), so you should by some way give some
value to foreign key field.

--
Sandeep Kaur
E-Mail: mkaur...@gmail.com
Blog: sandymadaan.wordpress.com
Reply all
Reply to author
Forward
0 new messages