Home > Back-end >  Can't add to ManyToManyField in Django custom task
Can't add to ManyToManyField in Django custom task

Time:02-04

I have two models in my Django app (Tag and MyModel).

MyModel has a ManyToManyField (tags) that uses the Tag model

class Tag(models.Model):

    CATEGORY_CHOICES = (
        ('topic', 'topic')
    )

    tag = models.CharField(max_length=100, unique=True)
    category = models.CharField(max_length=100, choices=CATEGORY_CHOICES)


class MyModel(models.Model):

   id = models.CharField(max_length=30, primary_key=True)
   title = models.CharField(max_length=300, default='')
   author = models.CharField(max_length=300)
   copy = models.TextField(blank=True, default='')
   tags = models.ManyToManyField(Tag)

I have a custom Django management task where I delete the Tags table, get new data, and refresh the Tags table with bulk_create

Tag.objects.all().delete()

....get new data for Tag table

Tag.objects.bulk_create(new_tags)

However after this - if I try to add a tag to an instance of MyModel.tags...

mymodel_queryset = MyModel.objects.all()

for mymodel in mymodel_queryset
    mymodel.tags.add(5)

...I get this error: CommandError: insert or update on table "bytes_mymodel_tags" violates foreign key constraint .... DETAIL: Key (tag_id)=(5) is not present in table "bytes_tag".

It seems like the Tag table is empty even though I just updated it

For some reason deleting and resetting the Tag table prevents me from adding to an instance of MyModel.tags. How can I do this?

Note: If I don't first delete and reset the Tags table then I can add to MyModel.tags just fine

CodePudding user response:

Instead of adding random number in mymodel.tags.add you have to add Tag object.

mymodel_queryset = MyModel.objects.all()

for mymodel in mymodel_queryset
    mymodel.tags.add(new_tags)

CodePudding user response:

Im expanding a little bit on what has already been said here above:

When you have a m2m relationship, if you want to create a new row in the through table by using add you have to do so by adding an instance of the corresponding class connected through the relationship.

So if you have Tag and MyModel and you want to add a Tag object to MyModel, you have to first create a MyModel instance and then add to it a Tag instance.

For example:

tag_1 = Tag.objects.create(tag="test", category="test") 
my_model_1 = MyModel.objects.create(title="my Title", author="me", copy="xxx")

# This is how you do it
my_model_1.add(tag_1)

Et voilà!

Note that when you use .add(instance) the save() method is built-in so you don't need to call it.

  •  Tags:  
  • Related