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?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/0a0f0614-dc75-4813-9542-f1d6de3134bf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to