1 year ago

#247733

test-img

Huw

Django ManyToManyField Model Manager

In my Python Django 3.7 source code I have the following models:

class ExampleForeignKey(models.Model):
    name = models.Charfield(max_length=200)
    fields = models.ManyToManyField('ExampleField', related_name='example_foreign_keys')

class ExampleField(models.Model):
    name = models.Charfield(max_length=200)

I experience an issue with database duplication when using threading with the source code:

example_field_obj = ExampleField(name='example_field_name')
example_foreign_key_obj = example_field_obj.example_foreign_keys.get_or_create(name='example_foreign_key_name')

I have managed to override the model manager get_or_create() method and applied a multithreading.Lock() in other cases where the model manager is being used directly in the case of direct 1:1 relationships however I don't know whether it is possible to apply the same principle in the case of ManyToManyField relationships?

Apologies if this has been asked about before elsewhere - I can't seem to find much information on this particular issue.

python

django

multithreading

django-models

manytomanyfield

0 Answers

Your Answer

Accepted video resources