Answer the question
In order to leave comments, you need to log in
How to add a ManyToManyField to a field?
I created a ManyToManyField field
class Picture(models.Model):
link = models.CharField('picture_link', max_length=200)
def __str__(self):
return self.link
class MyModel(models.Model):
picture = models.ManyToManyField(Picture, blank=True)
for pic in arr:
_picture = pic.text
try :
picture = Picture.objects.get(link = _picture)
except:
picture = Picture(link = _picture)
picture.save()
mymodel.picture.add(picture)
Cannot add "<Picture: https://nndd.com/wa-data/public/shop/products/29/13/1329/images/538/538.602x0.jpg>": instance is on database "None", value is on database "default"
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question