user3041764
user3041764

Reputation: 839

Django: How to organize model structure, one-model or relations between few models?

I have dilemma how to build my models structure. First of all I will explain my situation. I am selling goods on eBay basis on dropshipping, so when I have new order on ebay then I make order on my partner shop.

I would like create app where I can organize all orders from two platforms. I will use eBay API, PayPal API, PrestaShop API.

My question is: How to plan structure of models?

One model with no relations:

# eBay App
class Order(models.Model):
    date = models.DateTimeField(default=timezone.now, editable=False)
    paid = models.BooleanField(default=False)
    sales_manager_id = models.IntegerField(unique=True)
    shop_order_id = models.IntegerField(unique=True) # order ID from webshop
    shop_order_invoice = models.IntegerField(unique=True) # invoice num from webshop

Few models with relations:

# eBay App
class ebayOrder(models.Model):
    date = models.DateTimeField(default=timezone.now, editable=False)
    paid = models.BooleanField(default=False)
    sales_manager_id = models.IntegerField(unique=True)

# shop app
class shopOrder(models.Model):
    date = models.DateTimeField(default=timezone.now, editable=False)
    shop_order_id = models.IntegerField(unique=True) # order ID from webshop
    shop_order_invoice = models.IntegerField(unique=True) # invoice num from webshop
    ebay_order = models.ForeignKey(ebayOrder)

I must say that I have already created several applications in this project (I am generating ebay listings based on products in local db) so I will not create new app only for shopOrder needs.

UPDATE:

I presented only the basic version of my models. There will be more fields.

#eBay App
date, paid_date, amount, sales_manager_id, tracking_number, comment

#shop App
order_id, shop_order_id, invoice, products_amount, shipping_amount, shipping_date

#ebay App - customer_address model, shipping addres etc
order_id, nick, email, name, street1, street2, postcode, city, phone, state, country, company, vat, comment

Upvotes: 0

Views: 626

Answers (1)

I prefer one model without relationships. It is better for performance reasons, because you can grab all your data with only one SQL query. Actually, using Fat models is one of best practices in django dev. In your case, i am not sure if multiple entries of Order, may have the same date, paid, sales_manager_id fields. So every time a user updates his order, you create a new entry, copy the first 3 fields and fill the rest. If that is the case, then definitely break your model. It is bad practice to copy fields between entries in the same table.

Upvotes: 1

Related Questions