Re: Creating models from json url feed

2018-05-12 Thread Matt Snelgar
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.

On Sun, May 13, 2018 at 1:51 AM, Ryan Nowakowski <tuba...@fattuba.com>
wrote:

>
>
> On May 10, 2018 7:40:47 AM CDT, Matt Snelgar <snelg...@gmail.com> wrote:
> >
> >
> >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?
>
> 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?
>

-- 
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 django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
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/CAOaq%3D559npSb77r6VmaszpFx9GgZgANVQ7qZGsh5gRVrm_%3DZJQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Creating models from json url feed

2018-05-10 Thread Matt Snelgar


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 django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
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.