1. 程式人生 > >3-4 trade交易的model設計

3-4 trade交易的model設計

 

引用自己定義的UserProfile 有兩種方法:

一、

from django.db import models

from users.models import UserProfile


# Create your models here.
class ShoppingCart(models.Model):
    """
    購物車
    """
    user = models.ForeignKey(UserProfile)

二、

from datetime import datetime
from django.db import models
from django.contrib.auth import get_user_model

from goods.models import Goods
User = get_user_model()
# Create your models here.


class ShoppingCart(models.Model):
    """
    購物車
    """
    user = models.ForeignKey(User, verbose_name=u"使用者")

-------------------------------

C:\Users\huang\PycharmProjects\MxShop\apps\trade\models.py

 

from datetime import datetime
from django.db import models
from django.contrib.auth import get_user_model

from goods.models import Goods
User = get_user_model()
# Create your models here.


class ShoppingCart(models.Model):
    """
    購物車
    """
    user = models.ForeignKey(User, verbose_name=u"使用者")
    goods = models.ForeignKey(Goods, verbose_name=u"商品")
    goods_num = models.IntegerField(default=0, verbose_name=u"購買數量")
    add_time = models.DateTimeField(default=datetime.now, verbose_name="新增時間")

    class Meta:
        verbose_name = "購物車"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s(%d)".format(self.goods.name, self.goods_num)


class OrderInfo(models.Model):
    """
    訂單
    """
    ORDER_STATUS = (
        ("success", "成功"),
        ("cancel", "取消"),
        ("cancel", "待支付"),
    )
    user = models.ForeignKey(User, verbose_name=u"使用者")
    # 訂單編號
    order_sn = models.CharField(max_length=30, unique=True, verbose_name="訂單號")
    # 支付寶返回的訂單號
    trade_no = models.CharField(max_length=100, unique=True, null=True, blank=True, verbose_name="")
    pay_status = models.CharField(choices=ORDER_STATUS, max_length=10, verbose_name="訂單狀態")
    post_script = models.CharField(max_length=200, verbose_name="訂單留言")
    order_mount = models.FloatField(default=0.0, verbose_name="訂單金額")
    pay_time = models.DateTimeField(null=True, blank=True, verbose_name="支付時間")

    # 使用者資訊
    address = models.CharField(max_length=100, default="", verbose_name="收貨地址")
    signer_name = models.CharField(max_length=20, default="", verbose_name="簽收人")
    signer_mobile = models.CharField(max_length=11, verbose_name="聯絡電話")

    add_time = models.DateTimeField(default=datetime.now, verbose_name="新增時間")

    class Meta:
        verbose_name = u"訂單"
        verbose_name_plural = verbose_name

    def __str__(self):
        return str(self.order_sn)


class OrderGoods(models.Model):
    """
    訂單的商品詳情
    """
    order = models.ForeignKey(OrderInfo, verbose_name="訂單資訊")
    goods = models.ForeignKey(Goods, verbose_name="商品")
    goods_num = models.IntegerField(default=0, verbose_name="商品數量")

    add_time = models.DateTimeField(default=datetime.now, verbose_name="新增時間")

    class Meta:
        verbose_name = u"訂單的商品"
        verbose_name_plural = verbose_name

    def __str__(self):
        return str(self.order.order_sn)