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_together doesn't work seamlessly #7

Open
meshy opened this issue Aug 1, 2012 · 16 comments
Open

unique_together doesn't work seamlessly #7

meshy opened this issue Aug 1, 2012 · 16 comments

Comments

@meshy
Copy link
Collaborator

meshy commented Aug 1, 2012

If your sort_order field is unique_together with something, you need to add:

def validate_unique(self, exclude=None):
    f = 'sort_order'
    exclude = [f] if not exclude and f not in exclude else exclude + [f]
    return super(MyModel, self).validate_unique(exclude)

To your model.

@meshy meshy mentioned this issue Aug 1, 2012
7 tasks
@mjtamlyn
Copy link
Contributor

mjtamlyn commented Aug 2, 2012

WHY?

@jturnbull
Copy link
Contributor

Rewritten as a logical if statement…

if not exclude and 'sort_order' not in exclude:
    exclude = ['sort_order']
else:
    exclude = exclude + ['sort_order' ]

but, if exclude is None then how could sort_order be in exclude?

@meshy
Copy link
Collaborator Author

meshy commented Aug 2, 2012

Logic fail. Should be:

def validate_unique(self, exclude=None):
    if exclude is None:
        exclude = ['sort_order']
    elif 'sort_order' not in exclude:
        exclude = exclude + ['sort_order']
    return super(MyModel, self).validate_unique(exclude=exclude)

@jturnbull
Copy link
Contributor

Can this not be added to the Orderable object then?

@meshy
Copy link
Collaborator Author

meshy commented Aug 6, 2012

I am unsure of the implications of ignoring validation on this, especially as it only applies when sort_order is unique (which it is not, by default).

@jturnbull
Copy link
Contributor

Guess we just mention it in the README then

@meshy
Copy link
Collaborator Author

meshy commented Dec 4, 2014

An alternative syntax:

def validate_unique(self, exclude=None):
    exclude = exclude or []
    if 'sort_order' not in exclude:
        exclude.append('sort_order')
    return super().validate_unique(exclude=exclude)

@jturnbull
Copy link
Contributor

ZOMBIE thread…

@meshy
Copy link
Collaborator Author

meshy commented Dec 4, 2014

Still relevant unfortunately.

@meshy
Copy link
Collaborator Author

meshy commented Feb 23, 2015

To clarify, this is required because otherwise ModelForms (such as in the admin) will reject changes to the sort_order field where it is moved into a position that is currently in use.

@mjtamlyn
Copy link
Contributor

Note: this still sucks.

I think sort_order should always be unique or unique with another field. We could have a metaclass which inspects the model and raises warnings if this isn't the case. It's not a difficult migration to do for any model which has the wrong results at the moment.

@meshy
Copy link
Collaborator Author

meshy commented Feb 12, 2016

Perhaps this would be a good candidate for the Checks framework.

@maxpeterson
Copy link
Member

@meshy any reason not to add this to the Orderable model (with an additional check / constraint based self._meta.unique_together)? That way it could check if "If your sort_order field is unique_together with something" and use this logic?

@meshy
Copy link
Collaborator Author

meshy commented Mar 7, 2019

I don't see why not :)

maxpeterson added a commit that referenced this issue Mar 7, 2019
For #7

Add Orderable.validate_unique to exclude `sort_order` when it is
unique_together with something..
@maxpeterson
Copy link
Member

@meshy in terms of ensuring that "the sort_order field is either unique=True, or in unique_together"

... the only approach I can think of is to use a metaclass to dynamically add the sort_order field (if it is not added by the user) and set the unique=True based on whther the e sort_order field is in unique_together or not.

The biggest issue would be the need for migrations to be created before the library is updated. This could be documented and to mitigate the pain, the app could include a setting to disable this new feature (leaving the sort_order field unique=False) for legacy projects and / or to make it easier to migrate.

@maxpeterson
Copy link
Member

#53 mitigates the need to add validate_unique to a model that subclasses Orderable and has sort_order in unique_together.

Keeping this issue open to track the need to ensure the sort_order field is either unique=True, or in unique_together.

@maxpeterson maxpeterson reopened this Mar 8, 2019
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