Answer the question
In order to leave comments, you need to log in
Why are related fields not being submitted in the form?
hi there is a form
from django import forms
from .models import Item
class itemEdit(forms.ModelForm):
class Meta:
model = Item
fields = ('category', 'mode', 'type', 'delivery', 'title')
class Item(models.Model):
......
title = models.CharField(max_length=100,verbose_name=u'Название')
category = models.ForeignKey(Category, verbose_name=u'Укажите категорию')
mode = models.ManyToManyField(Modes, verbose_name=u'Выберите режимы')
......
def items_edit(request, id):
item = get_object_or_404(Item, id=id)
if request.method == "POST":
form = itemEdit(request.POST, instance=item)
if form.is_valid():
item = form.save(commit=False)
item.owner = request.user
item.published_date = timezone.now()
item.save()
return redirect('item.views.item_detail', id=item.id)
else:
form = itemEdit(instance=item)
return render(request, 'items/item_edit.html', {'form': form})
<form method="POST" class="post-form" enctype="multipart/form-data">{% csrf_token %}
{{ form|bootstrap }}
<button type="submit" class="save send">Сохранить</button>
</form>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question