Django (2.0) ForeignKey -> ManyToManyField

(0 comments)

So I just thought my small URL Todo django app (which I will poste the source of online soon - when I have some time to write up some README,.. too) could benefit from each urlTodo being able to be part of more than one category.

Now I already had some data in it so I was unsure. On IRC I was advised it might or might not work with automatic migrations. So I tested it.

Turns out it works ok, but does loose the assignement of the category (which was not an issue since  I did not have that much data in it yet).

so all I had to do was change

category = models.ForeignKey(Category,
                             on_delete=models.PROTECT,
                             related_name='urltodos',
                             null=False,
                             verbose_name=_('category'))

to

category = models.ManyToManyField(Category,
                                  related_name='urltodos',
                                  verbose_name=_('category'))

and run ./manage.py makemigrations and ./manage.py migrate and then assign the categories that got lost agian.

Again I am impressed how easily this could be done with django!

Currently unrated

Comments

There are currently no comments

New Comment

required

required (not published)

optional

required