从另一个中减去一个整数并保存

时间:2015-05-03 22:14:31

标签: django django-views

我需要从另一个整数中减去另一个整数并保存结果。它的某种付款方式。以前从不这样做,所以下面的代码 - 我在我看来试图做的事情:

def checkoutstatus(request, article_id):
    if request.POST:
        article = Article.objects.filter(id=article_id) 
        article.article_users.add(request.user)

        balance = int(UserProfile.objects.filter(balance=user_balance))
        cost = int(Article.objects.filter(cost=article_cost))
        new_balance = balance - cost

        article.save()

所以我首先将User连接到Article模型。然后,我需要从Article模型(article_cost扩展UserProfile)字段User中减去ForeignKey模型字段user_balance并将结果保存回来到user_balance ...

正如您在上面的代码中看到的那样,我尝试进行减法,但是如何将结果保存回user_balance呢?

此外,如果两个模型中的两个字段都已作为int工作,我是否需要IntegerField转换器?

我的应用article

from django.db import models
from django.contrib.auth.models import User
from django.db import models

class Article(models.Model):
    class Meta():
        db_table = 'article'

    article_users = models.ManyToManyField(User)
    article_title = models.CharField(max_length=200, blank=False, null=False)
    article_content = models.IntegerField(choices=CONTENT_CHOICES, null=True, blank=True)
    article_cost = models.IntegerField(default=0)
    article_likes = models.IntegerField(default=0)

我的应用userprofile

import PIL

from django.db import models
from django.contrib.auth.models import User
from PIL import Image
from django.db import models
from article.models import Article

class UserProfile(models.Model):  
    user = models.OneToOneField(User)
    user_picture = models.ImageField(upload_to='users', blank=False, null=False, default='users/big-avatar.jpg')
    user_balance = models.IntegerField(default=0)

User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u) [0])

我还创建了一个应用orderstatus,需要在DB中保存订单历史记录:

from django.db import models
from django.contrib.auth.models import User
from article.models import Article

class OrderHistory(models.Model):
    class Meta():
        db_table = 'order'

    user = models.ForeignKey(User)
    article = models.ForeignKey(Article)
    purchase_date = models.DateTimeField(auto_now_add=True)

并为其编写一个视图(您可以在该帖子的开头看到):

from django.shortcuts import render, render_to_response, redirect, Http404
from django.core.context_processors import csrf
from django.contrib import auth
from django.template import RequestContext
import datetime

from django.contrib.auth.models import User
from article.models import Article
from userprofile.models import UserProfile

def checkoutstatus(request, article_id):
    if request.POST:
        article = Article.objects.filter(id=article_id) 
        article.article_users.add(request.user)

        balance = int(UserProfile.objects.filter(balance=user_balance))
        cost = int(Article.objects.filter(cost=article_cost))
        new_balance = balance - cost

        article.save()

2 个答案:

答案 0 :(得分:2)

从MVT(模型视图模板)的角度来看,UserProfile的平衡修改应该在它自己不在视图中的模型中处理。

class UserProfile(models.Model):

    ## fields
    balance = models.IntegerField(default=0)


    def withdraw(self, amount):
        # you will need to check here if the 
        # balance is enough for this transaction or not
        self.balance = self.balance - amount


    def can_purchase_amount(self, amount):
        return True if amount <= self.balance

views.py

def checkoutstatus(request, article_id):
    """
    Make the transaction
    """
    user_profile = UserProfile.objects.get(user=request.user)
    article = Article.objects.filter(id=article_id)


    # check if the user can purhcase this
    if user_profile.can_purchase_amount(article.article_cost):
        # the user have enough balance to make this payment
        user_profile.withdraw(article.article_cost)
        user_profile.save()

        # add the user to the article
        article.article_users.add(request.user)

        # other things like register order or log

    else:
        # no enough balance

实际上,这个代码对于真钱交易来说并不完整,但它证明了谁应该做什么。在提交事务之前,您可能需要锁定用户的余额,以确保没有重复项。此外,您可能希望将此日志记录以供将来邀请

答案 1 :(得分:0)

执行此操作的一种方法是重写checkoutstatus,如下所示:

def checkoutstatus(request, article_id):
    if request.POST:
        article = Article.objects.filter(id=article_id) 
        article.article_users.add(request.user)

        balance = int(UserProfile.objects.filter(balance=user_balance))
        cost = int(Article.objects.filter(cost=article_cost))
        new_balance = balance - cost

        profile_to_update = UserProfile.objects.get(user=request.user)
        profile_to_update.user_balance = new_balance
        profile_to_update.save()

        article.save()

但这不是标准方式。查看@ Othman的答案,以获得更好的实施理念。