Django - ManyToManyField in model by setting it to null?

I have a django model (A) that has ManyToManyField (types) for another model (B). It is understood that the field in is "optional, restricting this object to these values." I installed blank=nulland null=Trueon ManyToManyField. I created an object from this model and set types for some values. Things are good.

I want to set it to "null", i.e. disable it. But in the django shell, the following errors occur:

>>> o.types.all()
[<Type: foo>, <Type: bar>]
>>> o.types = None
File "<console>", line 1, in <module>
File ".../virtualenv/lib/python2.6/site-packages/django/db/models/fields/related.py", line 627, in __set__
manager.add(*value)
TypeError: add() argument after * must be a sequence, not NoneType

I can successfully set it to [](empty list), but I would like to set it to None, since I want to use None as a signal / flag. Is this possible in Django models?

I am using Django 1.1

+5
source share
2 answers

I don’t think it’s possible to install it in None - think about how M2M is implemented at the SQL level (is this an intermediate table where you can write your β€œNone” for?).

If you need a separate flag, why not enter another column?

+2
source

All Articles