I have a model with some fields. Consider the model below.
class Item(models.Model):
a = models.CharField(max_length=200)
b = models.BooleanField(default=False)
c = models.CharField(max_length=200)
A form class for the corresponding model.
class ItemForm(ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class Meta:
model = Item
localized_fields = '__all__'
fields = [
'a',
'b',
'c',
]
And a view for the form. The view class uses a template which shows all the fields. But the field c
will be shown only if field b
is true.
{% if form.b.value %}
{% form.c %}
{% endif %}
field c
is not editable and I just want to show the value which is already pre-defined.
The issue is that if I use a if
condition in the template, (Assume b
is false) when I load the form it shows 2 fields a
and b
and when I submit the form with b
set to true, the form reloads without saving showing all three fields with the value of field c
as blank.
field c
already has a value and it is present in the DB but it doesn't show up. Why is that?
EDIT: Here is my view, pretty simple
class ItemGeneral(UpdateView):
form_class = ItemUpdateForm
template_name = 'pretixcontrol/item/index.html'
permission = 'can_change_items'
@transaction.atomic()
def form_valid(self, form):
# some code
return super().form_valid(form)