1
我有幾個使用ModelForms的UpdateView。每個對象都有一個用戶的外鍵,因此我可以將每個用戶的數據分開。當我嘗試在表單上更新數據,我得到:django - 將用戶標識添加到UpdateView
AttributeError的在/財務/交易/更新/ 4 「模塊」對象有沒有屬性「用戶」
創建對象的作品正好。這是更新給我的錯誤。
一個這樣的模型和視圖看起來是這樣的:
# Model
class Transaction(models.Model):
item_description = models.CharField(max_length=255)
payment_type = models.CharField(max_length=1, choices=PAYMENT_CHOICES, verbose_name="Payment Type")
amount = models.DecimalField(max_digits=8, decimal_places=2, verbose_name="Estimated Amount")
actual_amount = models.DecimalField(max_digits=8, decimal_places=2, verbose_name="Actual Amount")
due_date = models.DateField(verbose_name="Due Date")
is_credit = models.BooleanField(verbose_name="Is Asset")
is_paid = models.BooleanField(verbose_name="Paid/Received?")
account = models.ForeignKey(Account)
user = models.ForeignKey(User)
#ModelForm
class TransactionForm(ModelForm):
class Meta:
model = Transaction
fields = ['is_paid', 'item_description', 'due_date', 'amount', 'actual_amount', 'is_credit', 'account',]
#View
class TransactionUpdate(UpdateView):
model = Transaction
form_class = TransactionForm
template_name = 'finances/transaction_update.html'
def __init__(self, **kwargs):
self.kwargs = kwargs
def form_valid(self, form):
transaction = form.save(commit=False)
transaction.user = self.request.user
transaction.save()
return super(TransactionUpdate, self).form_valid(form)
感謝,