jpwatts / django-positions

A Django field for custom model ordering.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cannot use a manyToManyField for a collection

aronchi opened this issue · comments

If I try to add a collection named with a manyToManyField (like a category for a Product), I get this error:

'Product' instance needs to have a primary key value before a many-to-many relationship can be used.

I've used this code in my model: position= PositionField(_('position'), collection='categories')

Thanks for pointing this out; I've never tried using a ManyToManyField as a collection. It definitely doesn't work and the error message isn't helpful, either.

I'm going to generalize and say that any time you want to use a ManyToManyField as a collection, what you really need is an intermediate model with a PositionField. In your example, position is really an attribute of the relationship between a Product and a Category, rather than of a Product itself.

class Category(models.Model):
    ...

class Product(models.Model):
    categories = models.ManyToManyField(Category, through='ProductCategory')

class ProductCategory(models.Model):
    product = models.ForeignKey(Product)
    category = models.ForeignKey(Category)
    position = PositionField(collection=('product', 'category'))

If you can think of any cases where the generalization doesn't hold, I'd love to hear about them. Otherwise, I'll probably change PositionField to raise an exception if a ManyToManyField is provided in the collection argument. At least that way the error message will be related to the actual problem, rather than a Django implementation detail.

Closed by 650cb0e. Documents proper usage of PositionField for many-to-many relationships.

I documented this in 650cb0e. The example I gave above wasn't very good—sorry about that. I also added positions.examples.store to demonstrate proper usage and test that everything works as expected.