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

Unique over the three values #49

Closed
chrko opened this issue May 19, 2015 · 6 comments
Closed

Unique over the three values #49

chrko opened this issue May 19, 2015 · 6 comments
Milestone

Comments

@chrko
Copy link
Contributor

chrko commented May 19, 2015

In my current project I have the problem that I want the unique index over the three columns (from_, to, field.sort_value_field_name) in generated through model which is currently specified fix: 53322c4a[…]/sortedm2m/fields.py#L74

What should I do in this case?

Should be there an option three_column_unique?

@gregmuellegger
Copy link
Collaborator

I pushed something in the branch feature/customizable-intermediate-model. It
pulls the creation of the through model into the Field class and provides a
hook in the form of get_intermediate_model_attributes. I think it should
be possible with this hook to implement your custom unique_together with
something like this:

class MySortedM2M(SortedManyToManyField):
    def get_intermediate_model_attributes(self, attrs):
        attrs['Meta'].unique_together = (
            (attrs['_sort_field_name'], attrs['_to_field_name'], attrs['_from_field_name'],),
        )
        return attrs

Then use your custom field their and it should work well then.
Please give that a try and report back if that works for you. I'm happy to
merge these changes into master once you confirm that it solves your usecase.

@chrko
Copy link
Contributor Author

chrko commented May 19, 2015

Currently feature/customizable-intermediate-model is even with master?

@gregmuellegger
Copy link
Collaborator

Oops, I didn't push. Sorry! Should be there now.

@chrko
Copy link
Contributor Author

chrko commented May 20, 2015

Yes, this works. Thanks!

@gregmuellegger
Copy link
Collaborator

I did a new refactor of the model creation. So the example from above won't work any more as it had with the branch. However you can now hook into the get_intermediate_model_meta_class() method to accomplish your usecase.

I'm curious what you think of the refactor in a66b9ac

@gregmuellegger gregmuellegger added this to the 0.10.0 milestone May 21, 2015
@chrko
Copy link
Contributor Author

chrko commented May 21, 2015

I like the way to intercept the creation of the through model with hooks as of overiding some small methods.
And if you have now forgotten any corner case, the first step is done. 👍

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

2 participants