0

I have a many-to-many to many type models. I use a script to populate the database. However I want to print the objects and save it only if i want it to be saved using a y/n style input. The problem is I can't create the objects without saving them as you can see below.

>>> mov = Movie(name="completenothing")
>>> direc = Director(name="Someone")
>>> direc.movie_name.add(mov)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/username/Code/virtualenvironments/matrix/local/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.py", line 513, in __get__
    return self.related_manager_cls(instance)
  File "/home/username/Code/virtualenvironments/matrix/local/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.py", line 830, in __init__
    (instance, self.pk_field_names[self.source_field_name]))
ValueError: "<Director: Someone>" needs to have a value for field "id" before this many-to-many relationship can be used.
>>> direc.save()
>>> direc.movie_name.add(mov)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/username/Code/virtualenvironments/matrix/local/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.py", line 934, in add
    self._add_items(self.source_field_name, self.target_field_name, *objs)
  File "/home/username/Code/virtualenvironments/matrix/local/lib/python2.7/site-packages/django/db/models/fields/related_descriptors.py", line 1060, in _add_items
    (obj, self.instance._state.db, obj._state.db)
ValueError: Cannot add "<Movie: completenothing N/A>": instance is on database "default", value is on database "None"
>>> mov.save()
>>> direc.movie_name.add(mov)

Director and Movie are in a many-to-many relation and i want their information displayed before saving. Is there some mechanism to allow this ?

twitu
  • 553
  • 6
  • 12
  • What about a `pre_save` signal? That might be what you are looking for. – Josewails Jul 11 '18 at 05:54
  • Possible duplicate of [How to work with unsaved many-to-many relations in django?](https://stackoverflow.com/questions/518162/how-to-work-with-unsaved-many-to-many-relations-in-django) – Brown Bear Jul 11 '18 at 06:08
  • The information on how to work with m2m relationships in Django can be found in the [documentation](https://docs.djangoproject.com/en/2.0/topics/db/examples/many_to_many/). Once you understand that. It will be easy to apply that inside the `pre_save` signal. – Josewails Jul 11 '18 at 06:12
  • I don't think there is a direct method, the only work around is using model forms as given in the link by @BearBrown – twitu Jul 11 '18 at 17:20

1 Answers1

0

If you use a pre_save signal, you can try this.

from django.db.models.signals import pre_save



def confirm_save(sender, instance, **kwargs):

    # do something with your instance (display information)  

    ans = input("Do you want to save(y/n)")


    if ans == 'y':
        print("Your instance saved successfully")

    else:
        raise Exception("Not saved")

pre_save.connect(confirm_save, sender=MyModel)
Josewails
  • 570
  • 3
  • 16
  • saving is not the problem, it's that i want print the object before writing to database. But for many to m2m relation saving is required. – twitu Jul 11 '18 at 17:14