I am not setting the primary key manually. How could this be happening?
JavaScript
x
8
1
class Image(models.Model):
2
title = models.CharField(max_length=255)
3
image = models.ImageField(upload_to='attack_images', blank=True, null=True)
4
source_url = models.URLField(blank=True,null=True)
5
domain = models.ForeignKey(Domain, on_delete=models.CASCADE, blank=True, null=True)
6
creator = models.ForeignKey(User, on_delete=models.CASCADE, blank=True,null=True)
7
slug = models.SlugField(blank=True,null=True)
8
In my 5 years of programming in Django I’ve never gotten this error. I can’t tell what is going wrong by just looking at my code and the traceback. What could be wrong? Let me know if I need to post more code. Thanks in advance. Any help is appreciated.
Advertisement
Answer
You are trying to save your submodel in your save method.
JavaScript
1
5
1
self.image.save(
2
os.path.basename(self.source_url),
3
File(open(result[0], 'rb'))
4
)
5
Therefore you are saving your model before calling super’s save method. You should set your image but not save it before calling super’s save method.
JavaScript
1
2
1
self.image = os.path.basename(self.source_url),File(open(result[0], 'rb'))
2