title: Django objects.update_or_create
body: <p>i have a periodic_task running in celery that query for latest Cryptocurrency prices but for some reason, each time a want to display the data i dont get updated records i just get new ones and the old records are keep for some reason.</p>
  
  <p>tasks.py</p>
  
  <pre><code>@periodic_task(run_every=(crontab(minute='*/1')), name="Update Crypto rate(s)", ignore_result=True)
  def get_exchange_rate():
      api_url = "https://api.coinmarketcap.com/v1/ticker/"
      try:
          exchange_rates = requests.get(api_url).json()
          for exchange_rate in exchange_rates:
              CryptoPrices.objects.update_or_create(key=exchange_rate['id'],
                                                    symbol=exchange_rate['symbol'],
                                                    market_cap_usd=round(float(exchange_rate['market_cap_usd']), 3),
                                                    volume_usd_24h=round(float(exchange_rate['24h_volume_usd']), 3),
                                                    defaults={'value': round(float(exchange_rate['price_usd']), 3)}
                                                    )
          logger.info("Crypto exchange rate(s) updated successfully.")
      except Exception as e:
          print(e)
  </code></pre>
  
  <p>models.py</p>
  
  <pre><code>class CryptoPrices(models.Model):
      key = models.CharField(max_length=255)
      value = models.CharField(max_length=255)
      symbol = models.CharField(max_length=255)
      volume_usd_24h = models.CharField(max_length=255)
      market_cap_usd = models.CharField(max_length=255)
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code>def crypto_ticker(request):
          list_prices = CryptoPrices.objects.get_queryset().order_by('pk')
          paginator = Paginator(list_prices, 100)  # Show 100 prices per page
          page = request.GET.get('page')
          price = paginator.get_page(page)
          return render(request, 'crypto_ticker.html', {'price': price})
  </code></pre>
  
  <p>template.html:</p>
  
  <pre><code>{% extends 'base.html' %}
  {% load readmore %}
  
  {% block breadcrumbs %}
      {{ block.super }} » &lt;a href="{% url 'post_list' %}"&gt;Posts &lt;/a&gt; »
      &lt;a href="{% url 'crypto_ticker' %}"&gt;Crypto ticker&lt;/a&gt;
  {% endblock %}
  
  
  {% block content %}
  
      &lt;!DOCTYPE html&gt;
      &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Crypto ticker&lt;/title&gt;
      &lt;/head&gt;
  
      &lt;body&gt;
      &lt;h1 class="center"&gt;Crypto ticker&lt;/h1&gt;
      &lt;hr class="hr-style"&gt;
      &lt;div class="center"&gt;
          &lt;h4&gt;{{ prices }} Here you can find all frequently asked questions &lt;br&gt;
              if you still have still have any open points, please contact the &lt;a href="#"&gt;support&lt;/a&gt;.&lt;/h4&gt;
      &lt;/div&gt;
      &lt;br&gt;
      &lt;div class="paginator"&gt;
          &lt;span&gt;
           {% if price.has_previous %}
              &lt;a href="?page=1"&gt;&amp;laquo; First &lt;a&gt; |&lt;/a&gt;&lt;/a&gt;
              &lt;a href="?page={{ price.previous_page_number }}"&gt;Previous&lt;/a&gt;
          {% endif %}
  
          {% if price.has_next %}
              &lt;span&gt; Crypto prices - Page {{ price.number }} of {{ price.paginator.num_pages }}.&lt;/span&gt;
              &lt;a href="?page={{ price.next_page_number }}"&gt;Next&lt;a&gt; |&lt;/a&gt;&lt;/a&gt;
              &lt;a href="?page={{ price.paginator.num_pages }}"&gt;Last &amp;raquo;&lt;/a&gt;
          {% endif %}
         &lt;/span&gt;
     &lt;/div&gt;
      &lt;table class="table centercontentfloat class-three-box"&gt;
          &lt;thead&gt;
              &lt;tr style="font-size: small"&gt;
                  &lt;th&gt;Ranking&lt;/th&gt;
                  &lt;th&gt;Symbol&lt;/th&gt;
                  &lt;th&gt;Name&lt;/th&gt;
                  &lt;th&gt;Price&lt;/th&gt;
                  &lt;th&gt;Market Cap (USD)&lt;/th&gt;
                  &lt;th&gt;24 hrs. Volume (USD)&lt;/th&gt;
              &lt;/tr&gt;
          &lt;/thead&gt;
          &lt;tbody&gt;
          {% for price in price %}
              &lt;tr style="font-size: small"&gt;
                  &lt;td&gt;{{ price.id }}&lt;/td&gt;
                  &lt;td&gt;{{ price.symbol }}&lt;/td&gt;
                  &lt;td&gt;{{ price.key }}&lt;/td&gt;
                  &lt;td&gt;{{ price.value }} $&lt;/td&gt;
                  &lt;td style="font-size: small"&gt;{{ price.market_cap_usd }} $&lt;/td&gt;
                  &lt;td style="font-size: small"&gt;{{ price.volume_usd_24h }} $&lt;/td&gt;
              &lt;/tr&gt;
              {% endfor %}
          &lt;/tbody&gt;
      &lt;/table&gt;
      &lt;div class="paginator"&gt;
          &lt;span&gt;
              {% if price.has_previous %}
              &lt;a href="?page=1"&gt;&amp;laquo; First &lt;a&gt; |&lt;/a&gt;&lt;/a&gt;
              &lt;a href="?page={{ price.previous_page_number }}"&gt;Previous&lt;/a&gt;
          {% endif %}
  
          {% if price.has_next %}
              &lt;span&gt; Crypto prices - Page {{ price.number }} of {{ price.paginator.num_pages }}.&lt;/span&gt;
              &lt;a href="?page={{ price.next_page_number }}"&gt;Next&lt;a&gt; |&lt;/a&gt;&lt;/a&gt;
              &lt;a href="?page={{ price.paginator.num_pages }}"&gt;Last &amp;raquo;&lt;/a&gt;
          {% endif %}
         &lt;/span&gt;
      &lt;/div&gt;
  {% endblock %}
  </code></pre>
  
  <p>Is there any notable reason why the records i grab from the coinmarketcap api is getting saved/ displayed twice?</p>
  
  <p>if i go trough the pages i get back something like this:</p>
  
  <blockquote>
    <p>Crypto prices - Page 1 of 21. Next | Last »
    Ranking   Symbol  Name    Price   Market Cap (USD)    24 hrs. Volume (USD)
    1 BTC bitcoin 3795.6465 $ 66594617840.0 $ 8296474984.64 $
    2 ETH ethereum    143.9996 $  15106822040.0 $ 5043716023.22 $</p>
  </blockquote>
  
  <p>and on the second page:</p>
  
  <blockquote>
    <p>« First | Previous Crypto prices - Page 2 of 22. Next | Last »
    Ranking   Symbol  Name    Price   Market Cap (USD)    24 hrs. Volume (USD)
    101   BTC bitcoin 3798.3016 $ 66641201438.0 $ 8304474934.43 $
    102   ETH ethereum    144.0825 $  15115524904.0 $ 5048205218.98 $</p>
  </blockquote>
  
  <p>and i dont expect that BTC is also at the second page, only once at the first page?!?</p>
  
  <p>It really seems if i go trough the pages, that the records dont get updated, they will get saved one after another after each periodic_task run</p>
  
  <p>Kind regards</p>