Creating models from json url feed

32 views
Skip to first unread message

Matt Snelgar

unread,
May 10, 2018, 8:49:20 AM5/10/18
to Django users

I am making a django app to browse available products at different markets. I am getting data for my models from a JSON url feed, 

I am not sure on the best approach to turn this data into models as the feed for a single market can contain upwards of 50k items.


Models - feel free to suggest any improvements on these

    # Details of a sellable products
    class Product(models.Model):
        item = models.IntegerField(primary_key=True, auto_created=False)
        name = models.CharField(max_length=40)

    # Market where products can be bought, some markets will cater to multiple suburbs.
    class Market(models.Model):
        id = models.IntegerField(primary_key=True, auto_created=True)
        name = models.CharField(max_length=40)

    # Nearby suburbs can share the one market
    class Suburb(models.Model):
        name = models.CharField(max_length=30)
        market = models.ForeignKey(Market, on_delete=models.SET_NULL, null=True)

    # Actual product for sale
    class ProductForSale(models.Model):
        sale = models.IntegerField(primary_key=True)
        product = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True)
        cost = models.IntegerField(default=1)
        owner = models.CharField(max_length=30)
        quantity = models.IntegerField(default=1)
        market = models.ForeignKey(Market,on_delete=models.SET_NULL, null=True )
    Feed of markets products example

    "market_products": [
        {
            "id":11654,
            "item":123,
            "owner":"Bob",
            "suburb":"Springvale",
            "cost":3,
            "quantity":1,

        },
        {
            "id":11655,
            "item":123,
            "owner":"Sarah",
            "suburb":"Sunnyville",
            "cost":5,
            "quantity":2,

        },

This is how I am trying to populate my ProductForSale models - I don't know if this is the correct approach for the Product FK, or how to link the suburb to its Market FK

    markets = Market.objects.all()
    for market in markets:
        url = "jsonurl"
        response = urllib.request.urlopen(url)
        data = json.loads(response.read())
        for entry in data['market_products']:
                new_product_sale = ProductForSale.objects.create(
                    product = Product.objects.get(item=entry['item']),
                    cost = entry['cost'],
                    owner = entry['owner'],
                    quantity = entry['quantity']
                )
                new_product_sale.save()

Is there a point where I should create a separate model for each markets products, or is it fine to house them all in the one model?

Ryan Nowakowski

unread,
May 12, 2018, 1:51:38 PM5/12/18
to Matt Snelgar, Django users
Do products from different markets have many different fields? Or are most of the fields common?

Do you need to be able to filter on products across markets? Or are most of your product queries specific to a particular market?

Matt Snelgar

unread,
May 12, 2018, 9:48:22 PM5/12/18
to Ryan Nowakowski, Django users
All of the products will share the same fields.

I would filter on products, but my searches would only be restricted to one market at a time.

Ryan Nowakowski

unread,
May 13, 2018, 12:16:20 AM5/13/18
to Django users
I'd use a single model for all products from any market then.
Reply all
Reply to author
Forward
0 new messages