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.

Adding `num_min` parameter break order of orderable related object during creation only

See original GitHub issue

Issue Summary

During creation only, adding min_num parameter to InlinePanel breaks the order of orderable related object when extra forms are added by clicking “Add {obj_name}” button.

Steps to Reproduce

  1. Set min_num for InlinePanel for related links to any desirable number(e.g 2) in BlogPage class

from /wagtaildemo/demo/models.py lines: 423-429

BlogPage.content_panels = [
    FieldPanel('title', classname="full title"),
    FieldPanel('date'),
    StreamFieldPanel('body'),
    InlinePanel('carousel_items', label="Carousel items"),
    InlinePanel('related_links', label="Related links", min_num=2),
]
  1. In admin interface create a new “Blog page”
  2. Fill in 2 minimum required forms for related links (for visibility I titled links: ‘Link 1’, ‘Link 2’ etc)
  3. Click ‘add related links’ button to add extra form
  4. Fill in the extra form
  5. Repeat steps 4 and 5 N-times pre-save
  6. Save draft of the page
  7. After saving you can see that the order of related links is incorrect: extra links (Link 3 - Link N) become first links in the list and Link 1 and Link 2 moved to the bottom. post-save

It seems that adding min_num parameter breaks javascript. If min_num parameter is not set, the value attr of hidden input ‘ORDER’ for the first form in the formset is adding. However, when I add min_num parameter, the value attr of ‘ORDER’ for the first N required forms in the formset, is missing (is not adding), however for extra forms, the value of ‘ORDER’ is adding.

Technical details

Tried in 2 environments:

First
  • Python 3.4.3
  • Django==1.10.8
  • wagtail==1.12.2
  • Browser version: Chrome Version 61.0.3163.79 (Official Build) (64-bit)
Second
  • Python 2.7.12
  • Django==1.11.5
  • wagtail==1.11.1
  • Browser version: Chrome Version 61.0.3163.79 (Official Build) (64-bit)

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:2
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
DIGIRENcommented, Mar 25, 2020

Hey everybody. I was also having this issue, and while a fix on the actual CMS end will still be required at some point, this little hacky jQuery function should be enough to get you by. Just run it on page load on the admin side. This looks for any orderables that are missing that value=“” attr that the CMS does not generate when a min_num is set, and adds it.

function fixOrderables(){
    //Finding all inputs that contain -ORDER in their ID.
    $("input[id*='-ORDER").each(function(){
        if($(this).val() === ""){
            var input_id = $(this).attr("id");
            //Getting the number out of the ID and adding 1 to construct our new value.
            var input_val = parseInt(input_id.substr(input_id.indexOf("-ORDER")-1, 1))+1;
            if (parseInt($(this).val()) != input_val){
                $(this).val(input_val);
            }
        }
    });
 }
1reaction
lb-commented, Jan 26, 2018

IMHO - I like managing this on the serverside (option 1), reasoning is it is easier to test, more explicit in what we are providing to the clientside.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sorting an array of objects by property values - Stack Overflow
How do I create a function to sort the objects by the price property in ascending or descending order using JavaScript only? javascript...
Read more >
Built-in Types — Python 3.11.1 documentation
The following sections describe the standard types that are built into the interpreter. The principal built-in types are numerics, sequences, mappings, ...
Read more >
Restraining Orders - abuse_selfhelp - California Courts
These are orders telling the restrained person to move out from where the protected person lives and to take only clothing and personal...
Read more >
What you Need to Know About Protective Orders
They are legal documents issued by a judge or magistrate to protect the health and safety of a person who is alleged to...
Read more >
Opposing, Modifying, Dissolving, Or Appealing A Protection ...
You can file an opposition to the application or a motion asking the court to dissolve or modify the order, or you can...
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