2015-05-03 39 views
0

我需要減去另一個整數形式並保存結果。它的某種付款方式。從來沒有這樣做之前,所以下面的代碼 - 它是我想要在我看來,要做到:摘自另一個整數並保存

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() 

所以我第一次迷上了UserArticle模型。然後,我需要從UserProfile模型(擴展UserForeignKey)字段user_balance。減去Article模型場article_cost並保存結果返回給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

請提供您的'models.py',以便我們更好地瞭解範圍。另外,'user_balance'從哪裏來? – Hybrid

+0

@Hybrid發佈更新。提供所有的數據,這可以幫助你,我認爲... –

+0

'user_balance'沒有在你的視圖中提供! – Othman

回答

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 

事實上,這段代碼是無法完成一個真正的金錢交易,但它證明誰應該做什麼。在提交交易之前,您可能需要鎖定用戶的餘額,以確保沒有重複。您可能還需要記錄此爲未來的邀請

+0

Othman,最後,我得到了它的工作......)))Thx很多。另外,你能告訴我,如何顯示所有用戶的列表,哪些人連接到模板中的文章?我嘗試了'{{article.article_users.user.username}}'並沒有得到任何東西......正如我想的那樣,我需要爲某種東西循環? –

+1

因爲它是'ManyToManyField',那麼的確需要遍歷用戶。您可以使用字段名稱「{article for a article.article_users.all%}」來循環瀏覽購買此文章的用戶。 – Othman

+0

閱讀此https://docs.djangoproject.com/en/1.8/topics/db/examples/many_to_many/ – Othman

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() 

但這不是標準的方式。檢查@奧斯曼的回答是否有更好的實施想法。

相關問題