I am trying to change some database records from one legal state to another, but the intermediate (partially updated) state is not legal. As an example, suppose I model lectures, each of which consists of several short topics in some order:
class Lecture(models.Model): slug = models.TextField( help_text='Abbreviated name of lecture.' ) class Topic(models.Model): slug = models.TextField( help_text='Abbreviated name of topic.' ) lecture = models.ForeignKey( Lecture, help_text='The lecture this topic is part of.' ) order = models.IntegerField( help_text='Impose ordering on topics.' ) class Meta: unique_together = (('lecture', 'order'),)
My test case:
class TestTopicOrder(TestCase): def test_reordering_topics(self):
Essentially, I'm trying to do:
t0.order, t1.order = t1.order, t0.order
and then save, but no matter which of the changed objects I saved, the βorderβ will first have the same value as the other record. I could remove and redo it, but when the time comes to reorder several topics at once, it will be a pain. What is the cleanest way to do this?
source share