question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Optional sub-StructBlock not possible

See original GitHub issue

Given 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:open
  • Created 7 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
lacymorrowcommented, Aug 12, 2020

This seems like an important aspect of being able to use StructBlocks appropriately. Is there any timeline for scoping out work on this?

4reactions
gasmancommented, Jun 2, 2016

Hi @danni, You’re right that required has no effect on a StructBlock. 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 want featured_article.action to return None), but if ActionBlock had required=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 as ChoiceBlock 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) screen shot 2016-06-02 at 11 43 24

(after) screen shot 2016-06-02 at 11 42 41

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found