模型Arcitecture流程与钱包引荐和WalletLog

时间:2015-10-26 07:53:07

标签: python django-models foreign-key-relationship

任务是:如何将所有三个模型链接为我的要求

要求:如果用户转介给某人(other User),他的amount会收到一些wallet (Referral Based)。一个user可以refer到多个user

正如我上面提到的要求标准......请完整需要

我必须创建下面提到的三个模型,这些模型与User模型相关联。

1. Wallet
2. Wallet_Log
3. Referral

参见创建需要建议的模型..

class Wallet(models.Model):
    owner = models.OneToOneField(user, related_name='wallet', unique=True)
    amount = models.FloatField(default=0.0)
    datetime = models.DateTimeField(auto_now_add=True,null=True)
    walletID = models.CharField(max_length=10, default="00000")
    ack = models.BooleanField(default=False)

    def __unicode__(self):
            return u'%s' % self.owner

    @property
    def set_ack(self):
        return self.ack

    @set_ack.setter
    def set_ack(self, bol):
        self.ack = bol

class WalletLog(models.Model):
    wallet = models.ForeignKey(Wallet)
    user_from = models.ForeignKey(user, null=True, blank=True)
    amount = models.FloatField()   
    datetime = models.DateTimeField(auto_now_add=True, null=True)
    report = models.CharField(max_length=100)  


    def get_wallet_log(self):
        if(self.wallet):
            return "%s"%(self.wallet.amount)
        else:
            return "Not Available"

    def get_user_from_log(self):
        if(self.user_from):
            return "%s"%(self.user_from.user_first_name)
        else:
            return "Not Available"

    def __unicode__(self):
            return u'%s' % self.wallet


class OrderReferral(models.Model):

    referrer = models.ForeignKey(user, related_name='referrer')
    referred = models.ForeignKey(user, related_name='referred')
    wallet = models.ForeignKey(Wallet, related_name='order_referred')
    created_date = models.DateTimeField()
    updated_date = models.DateTimeField()
    order_active = models.BooleanField()

    def __unicode__(self):
        return '%s %s made a referral' % (self.referrer.first_name, self.referrer.last_name)


    class Meta:
        verbose_name_plural = "Order Referral"

1 个答案:

答案 0 :(得分:1)

是最后通过撰写model relationshipjoin query,以正确的方式设置test case以避免不必要的code