1

I am currently trying to copy a many-to-many-field from one model to another but running into a bit of trouble. I have been able to create the model fine with a many-to-many-field with a ModelMultipleChoiceField, and the model saves the way I want it to. When I try to copy it to another model, I don't get an error, but nothing happens. Here is the code that I have tried:

author =  Author.objects.create(author=self.author) 

author = Author.objects.all()[0]
book = entry.approvers.all()
author.pk = None
author.readers.add(*self.readers.all())

I researched this all am and see that M2M models can be tricky. I've tried several variations of the code above and nothing works. I worked my way through several must be an instance errors this am, and that's how I discovered the author = Author.objects.all()[0] command. I no longer get an error but my many-to-many values are not being copied over to another model either. Thanks for any help.

I found this reference on SO and it seems to be what would help me but I tried it and it doesn't work. I am using generic class based views which is probably what is causing me a bit of additional grief.

Class Based Views (CBV), CreateView and request.user with a many-to-many relation

From the example, I used this template for my code but to no avail.

I get needs to have a value for field "id" before this many-to-many relationship can be used.

I just found this reference, Django. Create object ManyToManyField error

It works, and I can then use the following command to post one of the M2M values, but how can I do this without hard coding the pk?

book.reader.add('1')

The above works, but when I try something like

book.reader.add(*self.readers.all())

I've researched this further and this...

author =  Author.objects.create(author=self.author) 

Plus this alone works...

book.reader.add('1')

Just need to figure out how to replace '1' with a variable name.

After more investigation this may be related to the following issue?

Django: IntegrityError during Many To Many add()

It would seem maybe there is some kind of bug with the bulk_create? The individual add with a hardcoded number works just fine. When I try with the (*self.readers.all()) I don't receive an error message but the manytomany reference is not copied over to the over table. I've seen several articles that say that the commands I'm using work just fine, but perhaps it's for something other than PostgreSQL.

Thanks for any thoughts.

As a newbie, I was doing this work in SAVE. Once I moved it to post_save, the following format worked as expected...

@receiver(post_save, sender='Test.Book')
  def post_save(sender,instance, **kwargs):

author = book.objects.create(subject=instance.book)
author.approvers.add(*instance.approvers.all())
Steve Smith
  • 1,019
  • 3
  • 16
  • 38
  • @kabanus I'm trying to copy using the code above. Creating the author object, and then trying to add the readers to the object. I've read that you can't save model with m2m you first have to create it and then add the m2m attributes after if I understand correctly. – Steve Smith Jan 24 '18 at 20:09
  • I made a newbie error. I was doing this work in the SAVE function for my model. When I moved the commands to a post_save function, the commands worked as expected. – Steve Smith Feb 01 '18 at 03:55
  • Good to see you figured it out. If you think in retrospect this isn't a useful question for the community I suggest you delete it. Otherwise post your findings as an answer, so others can benefit. – kabanus Feb 01 '18 at 06:36

1 Answers1

0

As a newbie, I was doing this work by overriding SAVE in my model instead of doing this work in a post_save signal. Changing my approach fixed my issue. The code associated with my correct approach is..

@receiver(post_save,sender='Test.Book')
def post_save(sender,instance,**kwargs):
    author = book.objects.create(subject=instance.book)
    author.approvers.add(*instance.approvers.all())
Steve Smith
  • 1,019
  • 3
  • 16
  • 38