question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't add ManytoMany fields to a Page

See original GitHub issue

For example

class BlogPage(Page):
    body = RichTextField()
    categories = models.ManyToManyField(BlogCategory, blank=True)

One gets ValueError “<BlogPage: foo>” needs to have a value for field “blogpage” before this many-to-many relationship can be used.

It’s caused by this https://github.com/torchbox/django-modelcluster/issues/3 https://github.com/torchbox/django-modelcluster/blob/master/modelcluster/forms.py#L260

I’ll keep trying to think of a solution. Seems like a common use case as Wordpress implements categories as a M2M (you can pick multiple, but they are predefined unlike a tag).

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:3
  • Comments:25 (9 by maintainers)

github_iconTop GitHub Comments

11reactions
gasmancommented, Feb 2, 2017

This is now implemented in django-modelcluster: https://github.com/torchbox/django-modelcluster/pull/62

From initial tests, it seems to be working nicely with Wagtail: just use ParentalManyToManyField instead of ManyToManyField, and specify it in a FieldPanel as normal.

Things that need to be done before the release of Wagtail 1.9:

  • release django-modelcluster 3.0 final and update wagtail’s setup.py to require django-modelcluster>=3.0,<4.0
  • add a release note to document the updated django-modelcluster / django-taggit dependencies (since it’s somewhat likely that people will have the old versions hard-coded in their requirements files)
  • ensure that M2M relations are covered by tests
  • document the use of ParentalManyToManyField, possibly through a new ‘categories’ section in the tutorial (which was dropped from https://github.com/wagtail/wagtail/pull/3125#issuecomment-259181336 because it was too messy to implement without real M2M support) as well as http://docs.wagtail.io/en/v1.8/topics/pages.html
4reactions
mattotscommented, Oct 9, 2014

I’ve just come up against exactly the scenario bufke raised in the initial comment - the need to provide an interface for adding categories to blog posts. The workaround using an inline is not very pretty, and tags don’t quite fulfil this particular use case. Is there really no proper M2M support in Wagtail? If not, can I add a massive +1 for this to be addressed please.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add data into ManyToMany field? - django
As that page states, you need to do: my_obj.categories.add(fragmentCategory.objects.get(id=1)). or my_obj.categories.create(name='val1').
Read more >
The right way to use a ManyToManyField in Django
To maintain a many-to-many relationship between two tables in a database, the only way is to have a third table which has references...
Read more >
I can't add Many-To-Many relation in Django
For many-to-many relationships add() accepts either model instances or field values, normally primary keys, as the *objs argument.
Read more >
Many-to-many field, save() method and the Django admin ...
When you save a model via admin forms it's not an atomic transaction. The main object gets saved first (to make sure it...
Read more >
4. How to show many to many or reverse FK fields on listview ...
You have been asked to show the childeren of each Hero, on the listview page. Hero objects have the children reverse FK attribute,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found