I am not setting the primary key manually. How could this be happening?
class Image(models.Model): title = models.CharField(max_length=255) image = models.ImageField(upload_to='attack_images', blank=True, null=True) source_url = models.URLField(blank=True,null=True) domain = models.ForeignKey(Domain, on_delete=models.CASCADE, blank=True, null=True) creator = models.ForeignKey(User, on_delete=models.CASCADE, blank=True,null=True) slug = models.SlugField(blank=True,null=True)
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.
self.image.save( os.path.basename(self.source_url), File(open(result[0], 'rb')) )
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.
self.image = os.path.basename(self.source_url),File(open(result[0], 'rb'))