0

Every time a run my Django app, the same data is inserted into database. I only want to include data the first time, when there are no values in base(table). I am getting my data from API and Json.

view.py

from django.shortcuts import render, HttpResponse
from .models import Brands
import requests

def get_brends(request):
    all_brands = {}
    url ='http://makeup-api.herokuapp.com/api/v1/products.json?brand'
    response = requests.get(url)
    data = response.json()
    for result in data:
         brand_data = Brands(
            name=result['name'],
            price=result['price'],
            image_link=result['image_link'],
            brand=result['brand'],
            category=result['category'],
            product_type=result['product_type'],
            description=result['description'],
        )
        brand_data.save()
        all_brands=Brands.object.all().order_by('-id')

    return render(request, 'brands_makeup/brands.html', { "all_brands":all_brands})

model.py

class Brands(models.Model):
    name = models.CharField(max_length=100, default="None")
    price = models.DecimalField(decimal_places=3,max_digits=4,null=True,blank=True)
    image_link = models.CharField( max_length=50, blank = True, null = True)
    brand = models.CharField( max_length=150, blank = True, null = True)
    category = models.CharField( max_length=150, blank = True, null = True)
    product_type = models.CharField( max_length=150, blank = True, null = True)
    description = models.CharField( max_length=1500, blank = True, null = True)

    def __str__(self):
        return self.name , self.price , self.category , self.brand

I am only trying to include JSON once in my database when a database is empty.

  • You can do an if test. Python will return `True` if there is object in the database. – Martins Jun 24 '21 at 13:06
  • The json file that you use includes `id` for every brand. I'd use that in my django model (by adding brand_id field). brand_id field will be unique with all items so if that's in the database, you don't have to save it. Also I'd use `bulk_create` to save it all into db at once. – Cagatay Barin Jun 24 '21 at 13:46

2 Answers2

1

try this :

def get_brands(request):
  check = Brands.objects.all()
  if check.count() == 0 :
    url ='http://makeup-api.herokuapp.com/api/v1/products.json?brand'
    response = requests.get(url)
    data = response.json()
    for result in data:
         brand_data = Brands(
            name=result['name'],
            price=result['price'],
            image_link=result['image_link'],
            brand=result['brand'],
            category=result['category'],
            product_type=result['product_type'],
            description=result['description'],
        )
        brand_data.save()
        all_brands=Brands.object.all().order_by('-id')

    return render(request, 'brands_makeup/brands.html', { "all_brands":all_brands})
  else:
    pass
G.S Dash
  • 78
  • 7
1

If I guessed right about your current situation, then maybe I can give you a help.

I think you want to your api to return brands in any case. And that's why you are saving data when the table is empty. In other words, your situation can be solved by providing default brand list in the table. In other words, seeding datatable in django is what you are really looking for.

If I am right, then I suggest you to visit this one. In Django, can you run seed data without always generating a migration?

Dharman
  • 30,962
  • 25
  • 85
  • 135
sirius9515
  • 309
  • 1
  • 8