2015-05-21 36 views
0

在我外鍵的出價「應用程序,我使用‘秩序’爲外鍵從‘訂單’應用作爲WSGIRequest」對象有沒有屬性‘user_slug’,而使用模型從多個應用

from orders.models import Order 
from sellers.models import SellerCompany 
class Bid(models.Model): 
user = models.ForeignKey(User) 
order = models.ForeignKey(Order) 
company = models.ForeignKey(SellerCompany) 
bid_price = models.DecimalField(max_digits=10, decimal_places=2) 
active = models.BooleanField(default=True) 

在我views.py 訂單應用程序中,我定義投標視圖

def bid(request, order_slug): 
if UserType.objects.is_seller(request.user): 
    form = BidForm(request.POST or None) 
    if form.is_valid(): 
     new_form = form.save(commit=False) 
     new_form.user = request.user 
     new_form.order.slug = order_slug 
     new_form.company.name = request.company.name 
     new_form.save() 
    return render_to_response('bids/bid.html', locals(), context_instance=RequestContext(request)) 
else: 
    messages.error(request, 'consumer account can not bid') 
    return HttpResponseRedirect('/') 

在我行

new_form.order.slug = request.order_slug 

它顯示了一個錯誤

'WSGIRequest' object has no attribute 'company' 

和其他錯誤是

Bid has no order. 

forms.py文件投標型號是

from django import forms 
from .models import Bid 
class BidForm(forms.ModelForm): 
class Meta: 
    model = Bid 
    fields = ('bid_price','active') 

通過這個練習中,我試圖order_slug分配到的爲了模型蛞蝓場。和公司的名稱SellerComapny誰是出價。

您的時間n幫助將不勝感激。

+0

你是否正在運行這個通過wsgi?它是一個開發服務器嗎? – Wtower

+0

我在** localhost **/**開發服務器上運行這個** ** – abdullah

回答

4

如果您想使用參數「order_slug」,則不必使用請求。你應該使用這個代碼:

new_form.order.slug = order_slug 
+0

我以前試過這個解決方案。那麼它說**出價沒有訂單。** – abdullah

+0

在這種情況下,問題可能出現在您的表單中。您正在收到此錯誤,因爲在變量「new_form」中沒有Bid對象,所以您有一個表單。 –

+0

檢查此鏈接,我認爲你可以找到一個解決方案看這裏:http://stackoverflow.com/questions/13438009/how-to-save-a-field-after-commit-false-with-model-form-in -django –

相關問題