Optional sub-StructBlock not possible
See original GitHub issueGiven a declaration like:
class FeaturedArticlesBlock(blocks.StructBlock):
heading = blocks.CharBlock(required=False)
articles = blocks.ListBlock(blocks.PageChooserBlock())
action = ActionBlock(required=False)
class ActionBlock(blocks.StructBlock):
text = blocks.CharBlock()
link = blocks.PageChooserBlock()
The action
field will always be expected to be provided in the admin, even though it is unrequired.
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Why should Java 8's Optional not be used in arguments
Although it might be tempting to consider Optional for not mandatory method parameters, such a solution pale in comparison with other possible alternatives....
Read more >26 Reasons Why Using Optional Correctly Is Not Optional
This means that the Supplier method passed as an argument is only executed when an Optional value is not present. So, this is...
Read more >Option Constructor? · Issue #14 · nlkl/Optional - GitHub
Hi,. Actually, the operation you mention is called SomeNotNull in Optional, and is available as an extension method: var option = myValue.
Read more >Optionals : are bad practices still bad practices if everyone ...
A container object which may or may not contain a non-null value. If a value is present, isPresent() will return true and get()...
Read more >No continuation on std::optional? : r/cpp - Reddit
Does anybody on here have any insight as to why the std::optional class does not come with a continuation function?
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This seems like an important aspect of being able to use StructBlocks appropriately. Is there any timeline for scoping out work on this?
Hi @danni, You’re right that
required
has no effect on aStructBlock
. Defining the semantics of ‘required-ness’ here would get a bit complicated: in your example above there’s only one valid interpretation of leaving the action blank (namely, ‘no action set’ - you’d wantfeatured_article.action
to returnNone
), but ifActionBlock
hadrequired=False
on all of its fields then it’s ambiguous whether leaving it empty means ‘no action’ (featured_article.action = None
) or ‘an action which happens to be blank’ (featured_article.action = {'text': '', 'link': None}
). There’s also the question of how to deal with fields that don’t have a concept of ‘being left blank’, such asChoiceBlock
dropdowns…Django has the same thing with formsets (if you leave a form blank, does that mean ‘no item’ or ‘an empty item’?), so it’s not impossible to solve - however, perhaps the neatest solution would be for a
required=False
StructBlock to be initially hidden, a bit like a ListBlock that only allows one item:(before)
(after)
That way, there’s an explicit UI state for “this StructBlock has been left unset” rather than having to infer it from whether or not its child fields have been filled in.