306

When you have a many-to-many relationship (related_name, not through) and you are trying to use the admin interface you are required to enter one of the relationships even though it does not have to exist for you to create the first entry.

I'm creating an app that is an event organizer. Imagine we had Event and Group models, bound with many-to-many relationship.

Django related_name creates another table with the indices of the two other tables.
But I see no reason why this extra table has to be populated.

If I work with the database through phpMyAdmin I can create a Group without registering an Event, since the connection between the two is only through a separate table, and there is no database value enforcement at given level.

How do I make the admin interface this realize it?
How do I make the many-to-many field optional in Django?

3cheesewheel
  • 9,133
  • 9
  • 39
  • 59
DZ.
  • 3,181
  • 2
  • 15
  • 10

2 Answers2

505

If you want to be able to specify ManyToMany relation without making it required just use blank=True:

class Group(models.Model):
    ...
    events = models.ManyToManyField(Event, blank=True)
Ludwik Trammer
  • 24,602
  • 6
  • 66
  • 90
  • 9
    This does solve the problem. Using the blank was not as obvious to me because I thought the manytomany created a table that links the events with the groups (events = models.ManyToManyField(Event, related_name="groups", blank=True) So it was not clear to use the blank since Event is not actually a field in the group table. Anyway it worked so thank you!!! – DZ. Mar 30 '10 at 03:36
  • You mean you have to use both `blank=True` and `null=True`? –  Oct 18 '12 at 16:52
  • 10
    @omouse I think yes, blank is for django validation, and null for the database. In this kind of case (like for IntegerField) it makes no sense to put one without the other I think – lajarre Oct 26 '12 at 15:46
  • 25
    Do you really need to have `null=True`? I was under the impression `null` doesn't have any affect on `ManyToMany` fields: http://stackoverflow.com/questions/18243039/south-migrating-foriegn-key-many-to-many-field-to-null-true-blank-true-doesnt – Tyler Hayes Oct 07 '13 at 04:16
  • 5
    Yeah, um @TylerHayes is right. null=True is basically a no-op and is nonsensical in the context of a ManyToMany. I'm open to being shown how I'm mistaken. – B Robster Nov 01 '13 at 17:17
  • 8
    null=True is not required, at least from Django 1.5 onwards. – Ville Laurikari Dec 06 '13 at 06:57
  • 2
    I use the "through" and get the error with "blank=true", when I had created a many to many entry and then do not need it any more (remove from dropdown). I then get "field required" after saving. – Timo Sep 18 '14 at 05:52
  • Is this working both ways ? For instance, do you have something to do in the Event model as well, so that Events can be created without having to specify a Group list ? – Overdrivr Aug 16 '18 at 13:53
  • @Overdrivr Of course. That would be true even if there wasn't `blank = True` here. – Ludwik Trammer Aug 17 '18 at 14:30
  • 2
    From django documentation null has no effect since there is no way to require a relationship at the database level. https://docs.djangoproject.com/en/2.1/ref/models/fields/#manytomany-arguments – Dee S Dec 05 '18 at 16:28
  • it courious if I set `blank=True, null=True` the Warning remains if I delete null=True it dissapears/ – titusfx Aug 05 '21 at 21:41
3

If Ludwik's answer does not solve the issue, set required to false in the serializer as well:

class RecipeDetailSerializer(RecipeSerializer):
    """Recipe detail serializer"""
    tags = TagSerializer(many=True, required=False)