jpwatts / django-positions

A Django field for custom model ordering.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Position changes after saving ModelForm without position field

jnm opened this issue · comments

commented

Hello,

After upgrading to 0.5.1, I noticed that when an object is saved via a ModelForm that does not include a position field, that object's position moves to last place. I think this is the responsible change: cfdb484#diff-d225dfacb4effdf5497b014c35da0d4eR92

I created a minimal project to demonstrate the issue using the models in the "photo" example. It's housed at https://github.com/jnm/django-positions-test, and the ModelForm I use there is:

class SillyForm(django.forms.ModelForm):
    class Meta:
        model = Photo 
        fields = ['name',]

In addition, if you are using update_fields, Django will throw an error (ValueError: Cannot use None as a query value) if the position field isn't included in the list of fields. Version 0.4.6 did not have this issue.

class UpdateForm(ModelForm):

    name = forms.CharField()

    def save(self, commit=True):
        instance = super(UpdateForm, self).save(commit=False)

        if commit:
            instance.save(update_fields=['name'])

        return instance

    class Meta:
        model = Item
        fields = ('name',)

To be clear, even if update_fields contains the position field (i.e. update_fields=['name','position']), the same behavior of moving to the last position is observed.

Hello!
How about this issue?
I had the same question.

Thanks!

D

I've verified that this is an issue, but haven't had time to get a fix together. It's probably obvious from how long this issue has been open, but I don't have much time to dedicate to this project at present. I'd be thrilled to merge the changes if anyone else has the resources to put together a pull request.

@jnm @chriskief do you guys have any workarounds for this?

commented

@jpadilla, I don't have anything good—I've just kept using version 0.5.0, which doesn't have the bug.

@jnm will try downgrading mine to 0.5.0 as well, thanks!

@jpadilla I've been using the previous version as well.

Same here. The last object saved gets the last position and all numbers being rebuilt. I didn't have time to look at it. Did anyone of those using django-position managed to solve the issue?

Update, solved in my fork. See the related pull request.