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

SortableStackedInlines change order #609

Closed
victorborrasdev opened this issue Apr 20, 2017 · 6 comments
Closed

SortableStackedInlines change order #609

victorborrasdev opened this issue Apr 20, 2017 · 6 comments

Comments

@victorborrasdev
Copy link

If this is a bug please specify versions you're using first.

Django version: 1.10.6
Django Suit version: 0.2.25
Python version: 2.7.13

Issue:
I have a SortableStackedInline in my admin and when saving it changes the order of everything, even without making any changes. It is true that i'm using some JS to hide/show values or even to filter select options, but I tried with SortableTabularInline and admin.StackedInline and I don't get this behavior.

For example, if I have 3 formsets with order 0, 1 and 2 and hit save, it will save two of them with orders 0 and 1, and the final result for ordering will be 0,0,1. If I hit save again, all order values will be 0. This value gets like this into the clean method of the form, but I don't know how to debug it in JS.

The code for the inline class is like this:

class MyModelInline(SortableStackedInline):
    model = MyModel
    extra = 0
    sortable = "order"

I have other fields, but this is the minimum configuration I tried.

If you need further information please let me know.

@darklow
Copy link
Owner

darklow commented Apr 20, 2017

I will need some more info to see where is the problem:

  1. Add following css to unhide order fields and make some screen recording?
.form-horizontal .hide {
 display: block !important;
}
  1. After you add css, could you do a screen recording to show what happens. I also need to see what fields are you using so that would help.

@victorborrasdev
Copy link
Author

Hi, here you have the recording. Inline formsets are big and don't fit in my screen, but after hitting save, all of their order fields get reduced by 1.

The fields in this recording are taken directly from the model, which has the following fields:

class StopRequest(AuthorityModel):
    biweekly_choices = (('1', _("First fortnight")),
                        ('2', _('Second fortnight')),)
    even_odd_weeks_choices = (('1', _('Odd weeks')),
                              ('2', _('Even weeks')))
    route_type_choices = (('1', _('Going')),
                          ('2', _('Return')),)
    order = models.PositiveIntegerField(default=0)
    student = models.ForeignKey(Student, verbose_name=_("Student"), related_name="stop_requests")
    route_type = models.CharField(max_length=10, verbose_name=_("Route type"), choices=route_type_choices)
    outward_route = models.ForeignKey(Route, related_name="outward_routes", verbose_name=_("Outward Route"), null=True,
                                      blank=True, limit_choices_to={"route_type": "1"})
    outward_stop = ChainedForeignKey(RouteStop, verbose_name=_("Outward Stop"), chained_field="outward_route",
                                     chained_model_field="route", show_all=False, auto_choose=True, sort=True,
                                     related_name="stop_outward_stop", null=True, blank=True)
    return_route = models.ForeignKey(Route, verbose_name=_("Return Route"), related_name="stop_return_route", null=True,
                                     blank=True, limit_choices_to={"route_type": "2"})
    return_stop = ChainedForeignKey(RouteStop, verbose_name=_("Return Stop"), chained_field="return_route",
                                    chained_model_field="route", show_all=False, auto_choose=True, sort=True,
                                    related_name="stop__return_stop", null=True, blank=True)
    all_grade = models.BooleanField(default=True, verbose_name=_("Entire school year"))
    date_start = models.DateField(verbose_name=_("Start date"), null=True, blank=True)
    date_end = models.DateField(verbose_name=_("End date"), null=True, blank=True)
    frequency = FrequencyField(verbose_name=_("Frequency"), default="1")
    weekdays = models.ManyToManyField(Weekday, verbose_name=_("Weekdays"), related_name="stop_requests", blank=True)
    month_part = models.CharField(max_length=1, verbose_name=_("Fortnights"), null=True, blank=True,
                                  choices=biweekly_choices)
    months = models.ManyToManyField(Month, verbose_name=_("Months"), related_name="stop_requests", blank=True)
    even_odd_week = models.CharField(max_length=10, verbose_name=_("Even/Odd weeks"), null=True, blank=True,
                                     choices=even_odd_weeks_choices)

    observation = models.TextField(max_length=1000, verbose_name=_("Observation"), blank=True, null=True)

And the AuthorityModel class from which this inherits has the following fields:

class AuthorityModel(models.Model):
    created_by = models.ForeignKey('AuthUser', editable=False, null=True,
                                   related_name='created_by_%(class)s', verbose_name=_('Created by'))
    modified_by = models.ForeignKey('AuthUser', editable=False, null=True,
                                    related_name='modified_by_%(class)s', verbose_name=_('Modified by'))

screen_recording.zip

@victorborrasdev
Copy link
Author

Hi, any news on this?

@smilemakc
Copy link

i have some bug

@realsby
Copy link

realsby commented Oct 11, 2017

I have same bug.. Tried lots of things.. Not working.. I belive frontend related issue..
In admin inline, when i use your css it shows all 0 inside hidden order values... I disable posting in javascript with preventdefault to see what happends right before submit.. Values still not changed at all.

Django (1.11.6)
django-suit (0.2.25)
django-suit-ckeditor (0.0.2)
Python 2.7.12

MODELS.py


class Project(models.Model):
    category = models.ForeignKey(Category, verbose_name=_('Category'))
    client = models.CharField(max_length=255, null=True, blank=True, verbose_name=_('Client'))
    organization = models.CharField(max_length=255, null=True, blank=True, verbose_name=_('Organization'))
    date = models.DateField(null=True, blank=True, verbose_name=_('Date'),)
    # more then 30 other column

    class Meta:
        ordering = ['order']

class ProjectPicture(models.Model):
    project = models.ForeignKey(Project, verbose_name=_('Project'))
    image = models.ImageField(upload_to=file_name, verbose_name=_('Image'))
    info_en = models.TextField(null=True, blank=True, verbose_name=_('Info English'))
    # more then 10 other column
    order = models.PositiveIntegerField(verbose_name=_('Order'))

    class Meta:
        ordering = ['order']

ADMINS.py


class ProjectPictureInlineForm(ModelForm):
    class Meta:
        model = ProjectPicture
        widgets = {
            'info_en': AutosizedTextarea,
        }
        fields = '__all__'


class ProjectPictureInline(SortableStackedInline):
    form = ProjectPictureInlineForm
    model = ProjectPicture
    extra = 1
    fk_name = 'project'
    sortable = 'order'
    list_editable = ('order', )


class ProjectAdmin(SortableModelAdmin):
    inlines = (ProjectPictureInline, )
    list_editable = ('show_as_vertical', 'order',)
    prepopulated_fields = {'slug_en': ('name_en',)}
    exclude = ('order',)
    sortable = 'order'

@realsby
Copy link

realsby commented Oct 11, 2017

I find the problem actualy, its on suit/static/js/sortable.js
Old versions works fine with a problem.. You have to fill new blank stacked lines with images etc..
I am trying to fix js to solve old issue properly

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

4 participants