title: What's the standard way of saving something only if its foreign key exists?
body: <p>I'm using Python 3.7 and Django . I have the following model, with a foreign key to another model ...</p>
  
  <pre><code>class ArticleStat(models.Model):
      objects = ArticleStatManager()
      article = models.ForeignKey(Article, on_delete=models.CASCADE, related_name='articlestats')
      ...
  
      def save(self, *args, **kwargs):
          if self.article.exists():
              try:
                  article_stat = ArticleStat.objects.get(article=self.article, elapsed_time_in_seconds=self.elapsed_time_in_seconds)
                  self.id = article_stat.id
                  super().save(*args, **kwargs, update_fields=["hits"])
              except ObjectDoesNotExist:
                  super().save(*args, **kwargs)
  </code></pre>
  
  <p>I only want to save this if the related foreign key exists, otherwise, I've noticed errors result.  What's the standard Django/Python way of doing something like this?  I thought I read I could use ".exists()" (<a href="https://stackoverflow.com/questions/11714536/check-if-an-object-exists">Check if an object exists</a>), but instead I get an error</p>
  
  <pre><code>AttributeError: 'Article' object has no attribute 'exists'
  </code></pre>
  
  <p><b>Edit:</b> This is the unit test I have to check this ...</p>
  
  <pre><code>    id = 1
      article = Article.objects.get(pk=id)
      self.assertTrue(article, "A pre-condition of this test is that an article exist with id=" + str(id))
      articlestat = ArticleStat(article=article, elapsed_time_in_seconds=250, hits=25)
      # Delete the article
      article.delete()
      # Attempt to save ArticleStat
      articlestat.save()
  </code></pre>