I have two models:
class Budget(models.Model):
name = models.CharField(max_length=25)
quantity = models.DecimalField(max_digits=10, decimal_places=2)
date = models.DateField(default=now)
datecompleted = models.DateTimeField(blank=True, null=True)
class Meta:
ordering = ['-date']
def __str__(self):
return self.name
class Expenses(models.Model):
name = models.CharField(max_length=25)
quantity = models.DecimalField(max_digits=10, decimal_places=2)
budget = models.ForeignKey(Budget, on_delete=models.CASCADE,
related_name='budget_expense')
category = models.ForeignKey(Category, on_delete=models.CASCADE)
date = models.DateField(default=now)
def __str__(self):
return self.name
class Meta:
ordering = ['-date']
and my Form as follow
class ExpenseForm(forms.ModelForm):
date = forms.DateTimeField(
widget=forms.widgets.DateTimeInput(attrs={'class': 'form-control', 'type': 'datetime-local'}),
label='Data', input_formats=['%d/%m/%Y %H:%M'])
class Meta:
model = Expenses
fields = ('name', 'quantity', 'budget', 'category', 'date')
and now I trying to create an instance of Expense with pre-filled (existing) Budget instance with below code:
def expense_add(request, budget_id=None):
if request.method == 'GET':
budget = get_object_or_404(Budget, pk=budget_id)
form = ExpenseForm(initial={'budget': budget})
return render(request, 'budget/expense-create.html', {'form': form})
else:
form = ExpenseForm(request.POST or None)
if form.is_valid():
form.save()
return redirect('list')
return render(request, 'budget/expense-create.html', {'form': form})
url pattern:
path('budget/<int:budget_id>/expense-create', views.expense_add, name='expense-create-new'),
path('budget/expense-create', views.expense_add, name='expense-create-new'),
but, as you may guess there is nothing in budget field inside my formfield.
P.S. budgetdetail.view:
def budget_detail(request, pk):
instance = get_object_or_404(Budget, pk=pk)
expenses_list = Expenses.objects.filter(budget_id=instance.pk)
budget_id = instance.pk
return render(request, 'budget/budget-detail.html',
{'instance': instance, 'expenses_list': expenses_list})
Please help. Thank you all!
CodePudding user response:
The problem is in the views.py where you set the initial value of budget. It expects either a budget object or the id of the budget object but you give the name of the object. So you can fix it by removing the .name
from the 4th line.
The corrected view:
def expense_add(request, pk):
if request.method == 'GET':
budget = get_object_or_404(Budget, pk=pk)
form = ExpenseForm(initial={'budget': budget})
return render(request, 'budget/expense-create.html', {'form': form})
else:
form = ExpenseForm(request.POST or None)
if form.is_valid():
form.save()
return redirect('list')
return render(request, 'budget/expense-create.html', {'form': form})
CodePudding user response:
Thank you all! In my url pattern there was another path('budget/<int:pk>/expense-create', ExpenseCreateView.as_view(), name='expense-create'),
which is cover or overwrite my view or something I suppose...once i comment it def expense_add
- worked!!