jrief / django-formset

The missing widgets and form manipulation library for Django
https://django-formset.fly.dev/
MIT License
326 stars 35 forks source link

DualSortableSelector not loading correctly #64

Closed vabene1111 closed 1 year ago

vabene1111 commented 1 year ago

Versions Django==4.2.1 django-formset==0.13.4

Description I have a django app that has a form that contains a DualSortableSelector widget according to the docs https://django-formset.readthedocs.io/en/latest/dual-selector.html#


# Form
class OpenDataStoreForm(forms.ModelForm):
    class Meta:
        model = OpenDataStore
        fields = ('slug', 'name', 'categories', 'comment')

        widgets = {
            'categories': DualSortableSelector(search_lookup='name__icontains'),
        }
# models
class OpenDataStore(OpenDataBaseModel):
    name = models.CharField(max_length=128, unique=True)
    categories = SortableManyToManyField(OpenDataCategory, through='tandoor.OpenDataStoreCategory')

class OpenDataStoreCategory(models.Model):
    store = models.ForeignKey(OpenDataStore, on_delete=models.PROTECT)
    category = models.ForeignKey(OpenDataCategory, on_delete=models.PROTECT)
    weight = models.BigIntegerField(default=0, db_index=True, )

    class Meta:
        ordering = ['weight']

class OpenDataProperty(OpenDataBaseModel):
    name = models.CharField(max_length=128, unique=True)
    unit = models.CharField(max_length=16, unique=True)

# Form View

class OpenDataStoreUpdate(UpdateView, FormView):
    template_name = "open_data/edit_template.html"
    model = OpenDataStore
    form_class = OpenDataStoreForm
<div class="row">
        <div class="col">
            <django-formset endpoint="{{ request.path }}" csrf-token="{{ csrf_token }}">

                {% render_form form "bootstrap" field_classes="mb-2" %}

                <button class="btn btn-success" type="button" click="submit -> proceed"><i
                        class="fal fa-save"></i> {% trans 'Save' %}</button>
            </django-formset>

        </div>
    </div>

The form can be opened, the categories can be assigned to the store and can be ordered and saved. The weight attribute is set in the db correctly according to the order that is choosen in the user interface.

The problem is when i re-open the form the DualSortableSelector widget loads the categories based on their ID order and not ordered by weight so that when I save the form again the order is lost.

Steps to reproduce

  1. order categories c1-c5 (ID corresponds to name) image
  2. database is correct image
  3. reload the form -> ordering is default (ID asc) again image

Note What is interesting is that other than on the screenshots in the docs when i click on an entry in the right table its not highlighted as a selected entry (but i can still sort it)

Thanks for this awesome library btw. its something I have always wanted and seems incredibly powerful and thank you in advance for any help.

jrief commented 1 year ago

This should be fixed in version 1.0.

Please try this example: https://django-formset.fly.dev/dual-selector/#sortable-dual-selector-widget

vabene1111 commented 1 year ago

thank you very much. I have in the meantime switched to another solution (not because of this bug but something else) so I cannot really test this anymore, but I will keep this awesome library in my mind for upcoming projects, its something I have always wanted for django and am really happy to know it exists.

jrief commented 1 year ago

I have in the meantime switched to another solution

may I ask to which one?

vabene1111 commented 1 year ago

i just used my usual vue frontend framework which is not really a framework but just some cobbled together input elements, so not really an alternative solution but a DIY approach

jamiecash commented 3 months ago

@jrief please reopen this issue. This appears to be an issue in version 1.5.

Saving writes the correct entries to the through table, however when reloading, the right hand side displays all available items, not those selected, and not in the correct order.

Every left hand side instance displays the same right hand instances in the same order, irrespective of whether they have been added.

Let me know if you would like the models, sql and output showing the correct database entries and screenshots showing the incorrect selected items.

jrief commented 3 months ago

@jamiecash please open a new issue and provide some code on how to reproduce this. Try to modify the specific example in testapp/ and fork this project.