I am trying to break up an existing model class. The original class is not optimal so I want to move all customer relevant information from CustomerOrder into a new class Customer. What is the best way to do this in Django?
Old model class:
class CustomerOrder(models.Model):
# Customer information fields
first_name = models.CharField(max_length=200) # Customer first name
last_name = models.CharField(max_length=200) # Customer last name
email = models.EmailField() # Customer email address
address = models.CharField(max_length=255) # Address to deliver (e.g. 1532 Commonwealth St. Apt 302)
city = models.CharField(max_length=200) # City to deliver (e.g. Fullerton, CA 92014)
# Order information fields
note = models.TextField() # Any notes the customer may have about shipping
shipping_method = models.CharField(max_length=200) # Shipping in LA or OC
total_price = models.FloatField(default=0) # Total price of the order
delivery_date = models.DateField() # When to deliver the order. Order is "live" until the next
# day after delivery. So if delivery date is Jan 3, it's "live" until Jan 4.
order_date = models.DateField() # When the customer ordered
time_slot = models.CharField(max_length=200) # What time to deliver the product
is_cancelled = models.BooleanField(default=False) # If the order is cancelled or refunded, we mark it here.
created_at = models.DateTimeField(auto_now_add=True) # When the order entry was saved into database
updated_at = models.DateTimeField(auto_now=True) # When the order was last updated in database
def __str__(self):
return self.first_name + " " + self.last_name
New model class:
class Customer(models.Model):
first_name = models.CharField(max_length=200) # Customer first name
last_name = models.CharField(max_length=200) # Customer last name
email = models.EmailField() # Customer email address
address = models.CharField(max_length=255) # Address to deliver (e.g. 1532 Commonwealth St. Apt 302)
city = models.CharField(max_length=200) # City to deliver (e.g. Fullerton, CA 92014)
There are duplicates in the old model so i want to remove those as well.
It depends on your database type. read this
You should be careful to dont loose your data!
I think the question is more to do with the whole relational database schema.
I would have all customer related stuff in one table just like the new CustomerOrder (rename this to Customer) class you have, then create another class for Orders then link the two with a one to many relationship. For example one customer can place many orders. If you want to implement this one to many relationship, simply add the following to the order class:
class Order(models.Model):
# The relavant order fields which you require, i.e. order number etc.
customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
Now when you create a new order instance, you can assign the customer. ps to access in reverse ie from customer to order you basically do customer.order_set() method (Customer can place many orders).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.