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)
感谢,