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

Inline sortable error when model features only FKs #232

Closed
heyscam opened this issue May 12, 2014 · 4 comments
Closed

Inline sortable error when model features only FKs #232

heyscam opened this issue May 12, 2014 · 4 comments

Comments

@heyscam
Copy link

heyscam commented May 12, 2014

I think there's an issue with the sortable JS. I have an inlined model which features only the parent FK, an FK to another model and the sortable order field. The final JS sort before submit is corrupting the order values. It only happens when the model has just these fields - I got round it by adding an extra, non FK field which I simply hide with CSS. I did try to amend the JS but I couldn't quite crack where it was going awry. Hoped you might be able to shed some light. Thanks v much for Suit BTW, it's awesome :-)

@darklow
Copy link
Owner

darklow commented May 12, 2014

Please provide a code example of your models and admin classes.
Also please specify Django version.

@mattbriancon
Copy link

I'm able to reproduce this issue with the following simple app (models taken almost directly from the Django docs).

# models.py
from django.db import models

class Person(models.Model):
    name = models.CharField(max_length=128)

    def __str__(self):
        return self.name

class Group(models.Model):
    name = models.CharField(max_length=128)
    members = models.ManyToManyField(Person, through='Membership')

    def __str__(self):
        return self.name

class Membership(models.Model):
    person = models.ForeignKey(Person)
    group = models.ForeignKey(Group)

    sort_order = models.PositiveIntegerField()

# admin.py
from django.contrib import admin
from suit.admin import SortableTabularInline
from .models import Person, Group, Membership

class PersonAdmin(admin.ModelAdmin):
    pass

class MembershipInline(SortableTabularInline):
    model = Membership
    sortable = 'sort_order'

class GroupAdmin(admin.ModelAdmin):
    inlines = [MembershipInline]

admin.site.register(Person, PersonAdmin)
admin.site.register(Group, GroupAdmin)

To reproduce, get the admin running and do the following:

  1. Add a few Person objects
  2. Create a new Group object
  3. While creating the Group, create one or more inline Membership objects.
  4. Click "Save"

You should see "This field is required." errors in the "Sort order" column.

screen shot 2014-06-11 at 19 51 47

After looking at #216, I tried the following and it seems to fix the problem but I'm not sure I understand what the implications of the change are.

--- sortables.js    2014-06-11 19:55:04.000000000 -0400
+++ sortables.fix.js    2014-06-11 19:55:30.000000000 -0400
@@ -132,7 +132,7 @@
                     var $set_block = $input.closest('.dynamic-' + fieldset_id);
                     if (!$set_block.length
                         || $set_block.hasClass('has_original')
-                        || $set_block.find(":input[value!=''][type!='hidden']").filter(filter_unchanged).serialize()) {
+                        || $set_block.find(":input[value!='']").filter(filter_unchanged).serialize()) {
                         value = i++;
                         $input.val(value);
                     }

Let me know if there's anything else I can do to help reproduce or fix the issue.

Thanks!

@darklow
Copy link
Owner

darklow commented Jun 19, 2014

Please try latest develop branch and confirm problem is fixed?

pip uninstall django-suit
pip install https://github.com/darklow/django-suit/tarball/develop

@mattbriancon
Copy link

Fixed! Thank you!

@darklow darklow closed this as completed Jun 19, 2014
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