Does this syntax for specifying Django conditional form display align with python/django convention?
Posted
by
andy
on Programmers
See other posts from Programmers
or by andy
Published on 2013-08-29T02:04:36Z
Indexed on
2013/11/10
22:22 UTC
Read the original article
Hit count: 284
I asked a similar question on Stackoverflow and was told it was better asked here. So I'll ask it slightly rephrased.
I am working on a Django project, part of which will become a distributable plugin that allows the python/django developer to specify conditional form field display logic in the form class or model class. I am trying to decide how the developer must specify that logic. Here's an example:
class MyModel(models.Model):
#these are some django model fields which will be used in a form
yes_or_no = models.SomeField...choices are yes or no...
why = models.SomeField...text, but only relevant if yes_or_no == yes...
elaborate_even_more = models.SomeField...more text, just here so we can have multiple conditions
#here i am inventing some syntax...i am looking for suggestions!!
#this is one possibility
why.show_if = ('yes_or_no','==','yes')
elaborate_even_more.show_if = (('yes_or_no','==','yes'),('why','is not','None'))
#help me choose a syntax that is *easy*...and Pythonic and...Djangonic...and that makes your fingers happy to type!
#another alternative...
conditions = {'why': ('yes_or_no','==','yes'),
'elaborate_even_more': (('yes_or_no','==','yes'),('why','is not','None'))
}
#or another alternative...
"""Showe the field whiche hath the name *why* only under that circumstance
in whiche the field whiche hath the name *yes_or_no* hath the value *yes*,
in strictest equality."""
etc...
Those conditions will be eventually passed via django templates to some javascript that will show or hide form fields accordingly.
Which of those options (or please propose a better option) aligns better with conventions such that it will be easiest for the python/django developer to use? Also are there other considerations that should impact what syntax I choose?
© Programmers or respective owner