Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in v2.2.4 ? #414

Open
jedie opened this issue Nov 25, 2024 · 7 comments
Open

Bug in v2.2.4 ? #414

jedie opened this issue Nov 25, 2024 · 7 comments

Comments

@jedie
Copy link
Contributor

jedie commented Nov 25, 2024

I seems to me, that v2.2.3 -> v2.2.4 introduces a bug: Adding a new entry to a existing object will add it not always as a new last entry... Sometimes it will be added as the second one.

I found this bug, because a integration tests failed. It looks like:

#...

response = self.client.post(
    path='/admin/foo/add/',
    data=get_test_form_data(
        title='A Test Entry',
        **{
            'chapters-TOTAL_FORMS': '1',
            'chapters-0-title': 'Chapter One',
            'chapters-0-position': '0',
        },
    ),
)
test_item = Foo.objects.get()
self.assertEqual(test_item.title, 'A Test Entry')

#...

response = self.client.post(
    path=f'/admin/foo/{test_item.pk}/change/',
    data=get_test_form_data(
        title='A Test Entry',
        **{
            'chapters-TOTAL_FORMS': '2',
            'chapters-INITIAL_FORMS': '1',
            'chapters-0-id': str(chapter1.pk),
            'chapters-0-test': str(test_item.pk),
            'chapters-0-title': 'Chapter One',
            'chapters-0-position': '1',
            'chapters-1-test': str(test_item.pk),
            'chapters-1-title': 'Chapter Two',
            'chapters-1-position': '0',
            'chapters-__prefix__-test': str(test_item.pk),
            'chapters-__prefix__-position': '0',
        },
    ),
)

test_item.refresh_from_db()
self.assertEqual(
    list(test_item.chapters.values_list('title', flat=True)), ['Chapter One', 'Chapter Two']
)

#...

With v2.2.3 it's correct: ['Chapter One', 'Chapter Two']
With v2.2.4 it's: ['Chapter Two', 'Chapter One']

@jrief
Copy link
Owner

jrief commented Nov 25, 2024

@ldeluigi may it be that this issue is a regression from merging PR #413?

@ldeluigi
Copy link
Contributor

Didn't we just change the save_new method? It's not supposed to be invoked with updates I think

@jrief
Copy link
Owner

jrief commented Nov 26, 2024

@jedie can you please test, if reverting PR #413 solves your problem? We then need a strategy for a unit test so that this regression does not occur anymore. Could you please adopt the current testapp to reflect this?

@ldeluigi
Copy link
Contributor

Btw, now that I'm looking again it seems that the test @jedie is running is a false negative: in the form data, they are uploading "Chapter One" with position 1 and "Chapter Two" with position 0. Thus, the result containing "Chapter Two" first and "Chapter One" second should be considered correct.

@jedie
Copy link
Contributor Author

jedie commented Nov 26, 2024

This is exactly what the browser also send. Or exists the bug in the JavaScript part that adds the field?!?

Think the playwright tests should cover this, isn't it?

@ldeluigi
Copy link
Contributor

ldeluigi commented Nov 26, 2024

It's not a bug, it's how the library works. Even if you change the order of an item in the admin page, "Chapter One" will always have index 0 written in the form field labels, while its position field will change value according to its position. This means that it's the position field to be responsible for determining the position of Chapter One, even if the index is of the item inside the form fields is still 0.

@ldeluigi
Copy link
Contributor

From an HTML perspective, chapters-0-test is just a label and the 0 is not an index at all.

PS: I've tested the latest version myself and to me it's working as intended

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants