我已经做了一个简单的商店,用户可以在他的个人资料中填充余额,然后可以购买一些数字内容。 现在我需要保存所有购买在一个单独的模型,在那里我可以看到,谁买了什么,什么时候... 但我不明白,如何在用户购买物品后在模型中保存该信息... 是我现在...创建购物历史
与平衡用户模式和退出功能:
class UserProfile(models.Model):
class Meta():
db_table = 'userprofile'
user = models.OneToOneField(User)
user_picture = models.ImageField(upload_to='users', blank=False, null=False, default='users/big-avatar.jpg')
user_balance = models.DecimalField(default=0, max_digits=10, decimal_places=2)
def withdraw(self, amount):
self.user_balance = self.user_balance - amount
def can_purchase_amount(self, amount):
if amount <= self.user_balance:
return True
User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u) [0])
的orderstatus
的应用程序,在那里我做了一个模型OrderHistory
订货历史:
class OrderHistory(models.Model):
class Meta():
db_table = 'order_history'
user = models.ForeignKey(User)
article = models.ForeignKey(Article)
purchase_date = models.DateTimeField(auto_now_add=True)
一压脚提升用户打“买入”,他推出POST
与action
,在orderstatus
应用程序,遵循以views.py
:
def checkoutstatus(request, article_id):
user_profile = UserProfile.objects.get(user=request.user)
article = Article.objects.get(id=article_id)
if user_profile.can_purchase_amount(article.article_cost):
user_profile.withdraw(article.article_cost)
user_profile.save()
article.article_users.add(request.user)
return redirect('/articles/get/%s' % article_id)
所以,这种观点的检查是用户有足够的钱在他的user_balance
,如果是,做一个withdraw
。因此,如果购买完成,我需要在OrderHistory
型号中保存该购买...从未做过此类任务......我该怎么做?
哦......不要以为有些事情有时候这么简单......)))谢谢。 –