improve south support
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
django-sortedm2m |
New
|
Undecided
|
Unassigned |
Bug Description
(is there a way for me to mark this as a whishlist bug?)
south is a widely used database migration framework for django. It would be nice if sortedm2m would support south out-of-the-box. This would involve adding a bit of code to fields.py (introspection rules south uses), and possibly fixing bugs.
1) south needs introspection rules (see http://
-------
try:
from south.modelsins
add_
(
[],
},
),
], ["^sortedm2m\
except ImportError:
pass
-------
I think the best place to put this would be the end of fields.py
2) The following models.py works with the introspection rules above:
-------
class Book(models.Model):
title = models.
class Shelf(models.
books = SortedManyToMan
-------
However, putting the two models in reverse order, like this:
-------
class Shelf(models.
books = SortedManyToMan
class Book(models.Model):
title = models.
-------
Triggers the following errors when the migation is run:
-------
Error: One or more models did not validate:
myapp.shelf: 'books' has an m2m relation with model <class 'myapp.
myapp.shelf: 'books' specifies an m2m relation through model <class '.myapp_
myapp.shelf: Accessor for m2m field 'books' clashes with related m2m field 'Book.shelf_set'. Add a related_name argument to the definition for 'books'.
myapp.shelf: 'books' has an m2m relation with model <class 'myapp.
myapp.shelf: 'books' specifies an m2m relation through model <class '.myapp_
myapp.shelf: The model Shelf has two manually-defined m2m relations through the model myapp_Shelf_books, which is not permitted. Please consider using an extra field on your intermediary model instead.
myapp.shelf: Accessor for m2m field 'books' clashes with related m2m field 'Book.shelf_set'. Add a related_name argument to the definition for 'books'.
-------
This might be caused by a problem in south, I didn't investigate further. I did, however try an other workaround for this (reordering the model classes like in the first, working, example being one possibility): First creating a migration which adds the Book model, and adding the Shelf model in a second migration. In this case, I got the following exception when the migration was created:
-------
$ python manage.py schemamigration myapp add_shelf --auto
<class 'myapp.
<class 'myapp.
+ Added model myapp.Shelf
Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_
File "/mnt/data/
utility.
File "/mnt/data/
self.
File "/mnt/data/
self.
File "/mnt/data/
output = self.handle(*args, **options)
File "/mnt/data/
action.
File "/mnt/data/
forwards.
File "/mnt/data/
"left_field": self.field.
TypeError: 'NoneType' object is unsubscriptable
-------
From my previous comment: south.aeracode. org/docs/ customfields. html#extending-
> 1) south needs introspection rules (see http://
> introspection), the following appear to work (with south 0.7.1 and trunk version of sortedm2m)
Actually, it doesn't work properly: the migration generated by south contains the following: ------- -------
db.create_ table(' myapp_shelf_ books', ( AutoField( verbose_ name='ID' , primary_key=True, auto_created= True)),
(' shelf', models. ForeignKey( orm['myapp. shelf'] , null=False)),
(' book', models. ForeignKey( orm['myapp. book'], null=False)) ------- -------
-------
# Adding M2M table for field books on 'Shelf'
('id', models.
))
-------
Which means that the sort_value column is not recognized by south, which will cause problems later on, since the column won't be created in the database