Dear Django fellows,

using Django 1.8.3, in a fully migrated app I have a model with a many-to-many relationship like this:

    from django.db import models
    from django.contrib.auth.models import User

    class Bereich(models.Model):
        benutzer = models.ManyToManyField(User)

Now I would like to store some extra information with the relationship (e.g. some Booleans that describe what the user is permitted to do with the related Bereich object).
That is, the intended result is planned to look like this:

    class Bereich(models.Model):
        benutzer = models.ManyToManyField(User, through='UserBereichAssignment')

    class UserBereichAssignment(models.Model):
        bereich  = models.ForeignKey(Bereich)
        user     = models.ForeignKey(User)
        can_edit = models.BooleanField(verbose_name="can edit?", default=True)

However:

    $ ./manage.py makemigration         # ok, creates a new migrations file
    $ ./manage.py migrate

    # ...
ValueError: Cannot alter field Lori.Bereich.benutzer into Lori.Bereich.benutzer - they are not compatible types (you cannot alter to or from M2M fields, or add or remove through= on M2M fields)


Well, I understand that and also seem to see some of the involved problems: For example, how would it make sure that the same database table is used for the previous implicit intermediate model and the new UserBereichAssignment?

I'm also aware of https://code.djangoproject.com/ticket/23034, but I'm not sure what to make of it, or if it is even applicable here.


Thus, what would be a good way to proceed?

To the best of my understanding, I could do this manually, that is: create an entirely new many-to-many field, e.g.

    class Bereich(models.Model):
        benutzer  = models.ManyToManyField(User)                # old, unchanged
        benutzer_ = models.ManyToManyField(User, 
through='UserBereichAssignment')

then manually migrate the data from "benutzer" to "benutzer_" (possibly using a data migration?), and finally remove the old field "benutzer". The only downside seems that all dependent code had to be updated from using the old name "benutzer" to the new name "benutzer_". (I'm not sure if making a migration for renaming the new field name to the old field name is possible?)

Alas... are there any viable alternatives to this?
I'd be very grateful for any hint!   :-)

Best regards,
Carsten

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/55A7BA3E.5030105%40cafu.de.
For more options, visit https://groups.google.com/d/optout.

Reply via email to