1
0
mirror of https://github.com/django/django.git synced 2025-10-27 07:36:08 +00:00

Add unique_together altering operation

This commit is contained in:
Andrew Godwin
2013-07-02 11:19:02 +01:00
parent 310cdf492d
commit 67dcea711e
7 changed files with 84 additions and 17 deletions

View File

@@ -1,2 +1,2 @@
from .models import CreateModel, DeleteModel, AlterModelTable
from .models import CreateModel, DeleteModel, AlterModelTable, AlterUniqueTogether
from .fields import AddField, RemoveField, AlterField, RenameField

View File

@@ -7,7 +7,7 @@ class AddField(Operation):
"""
def __init__(self, model_name, name, field):
self.model_name = model_name
self.model_name = model_name.lower()
self.name = name
self.field = field
@@ -33,7 +33,7 @@ class RemoveField(Operation):
"""
def __init__(self, model_name, name):
self.model_name = model_name
self.model_name = model_name.lower()
self.name = name
def state_forwards(self, app_label, state):
@@ -62,7 +62,7 @@ class AlterField(Operation):
"""
def __init__(self, model_name, name, field):
self.model_name = model_name
self.model_name = model_name.lower()
self.name = name
self.field = field
@@ -93,7 +93,7 @@ class RenameField(Operation):
"""
def __init__(self, model_name, old_name, new_name):
self.model_name = model_name
self.model_name = model_name.lower()
self.old_name = old_name
self.new_name = new_name

View File

@@ -9,7 +9,7 @@ class CreateModel(Operation):
"""
def __init__(self, name, fields, options=None, bases=None):
self.name = name
self.name = name.lower()
self.fields = fields
self.options = options or {}
self.bases = bases or (models.Model,)
@@ -35,7 +35,7 @@ class DeleteModel(Operation):
"""
def __init__(self, name):
self.name = name
self.name = name.lower()
def state_forwards(self, app_label, state):
del state.models[app_label, self.name.lower()]
@@ -58,7 +58,7 @@ class AlterModelTable(Operation):
"""
def __init__(self, name, table):
self.name = name
self.name = name.lower()
self.table = table
def state_forwards(self, app_label, state):
@@ -78,3 +78,33 @@ class AlterModelTable(Operation):
def describe(self):
return "Rename table for %s to %s" % (self.name, self.table)
class AlterUniqueTogether(Operation):
"""
Changes the value of unique_together to the target one.
Input value of unique_together must be a set of tuples.
"""
def __init__(self, name, unique_together):
self.name = name.lower()
self.unique_together = set(tuple(cons) for cons in unique_together)
def state_forwards(self, app_label, state):
model_state = state.models[app_label, self.name.lower()]
model_state.options["unique_together"] = self.unique_together
def database_forwards(self, app_label, schema_editor, from_state, to_state):
old_app_cache = from_state.render()
new_app_cache = to_state.render()
schema_editor.alter_unique_together(
new_app_cache.get_model(app_label, self.name),
getattr(old_app_cache.get_model(app_label, self.name)._meta, "unique_together", set()),
getattr(new_app_cache.get_model(app_label, self.name)._meta, "unique_together", set()),
)
def database_backwards(self, app_label, schema_editor, from_state, to_state):
return self.database_forwards(app_label, schema_editor, from_state, to_state)
def describe(self):
return "Alter unique_together for %s (%s constraints)" % (self.name, len(self.unique_together))