C
C
Cyber_bober2016-10-26 14:25:38
Django
Cyber_bober, 2016-10-26 14:25:38

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'Выберите режимы')
    ......

When submitting, the title is sent in order, but the related fields are not sent( What could be the reason, where to dig?
I send like this
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})

Here is the form code
<form method="POST" class="post-form" enctype="multipart/form-data">{% csrf_token %}
            {{ form|bootstrap }}
            <button type="submit" class="save send">Сохранить</button>
</form>

Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri, 2016-10-26
@Ba1t

Because you are using a different form in the view?

class EditForm(forms.ModelForm):
...
form = itemEdit(request.POST, instance=item)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question