How do I make many-to-many field optional in Django?

DjangoDjango AdminMany to-Many

Django Problem Overview


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?

Django Solutions


Solution 1 - Django

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDZ.View Question on Stackoverflow
Solution 1 - DjangoLudwik TrammerView Answer on Stackoverflow