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

DocumentFormSet modification leads to NotUniqueError #75

Open
MisterFreeze opened this issue Nov 20, 2014 · 1 comment
Open

DocumentFormSet modification leads to NotUniqueError #75

MisterFreeze opened this issue Nov 20, 2014 · 1 comment

Comments

@MisterFreeze
Copy link

First, let me thank you for this nice library.

I am migrating a project from django models to django mongoengine documents and I am using django-mongodbforms 0.3 as a replacement for the django forms for models. The migration is seamless so far.

I just tried to update a set of forms constructed from a supplied queryset in this way:

    formset_constructor = document_formset_factory(document_class, max_num=1, **formset_kwargs)
    qs = docucment_class.objects.filter(id=obj_id)
    formset_check = formset_constructor(request.POST, queryset=qs)
    if formset_check.is_valid():
        for inst in formset_check.save(commit=False):
            # do something
            inst.save()

The document class has a unique constraint on a field and the inst.save() raises a NotUniqueError (for that field, if value is unchanged).

I think this happens because the instance is freshly constructed during the is_valid() execution and the id value is not copied to that new instance in construct_instance(), which is called by _post_clean().
In fields_for_document(), any ObjectIdField is skipped. ObjectIdFields are skipped in construct_instance() as well. When I remove

    if isinstance(f, ObjectIdField):
        continue

in both functions, the inst.save() succeeds without raising that NotUniqueError.

I think that this is a common use case. Did I do something wrong here?

Best regards,

Chris

@MisterFreeze
Copy link
Author

Seriously, did I do something wrong here or have an incorrect understanding of how this should work? If not, I see this as a bug that I'm very wliling to create a pull request for.

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

1 participant