django forms post request raising an error on __init__ method

怎甘沉沦 提交于 2021-02-10 15:57:16

问题


I have a django form which takes a paramater from the view to initialize the MultipleChoiceField based on the user instance.

The form is working fine when loading the template. when i submit the form the init method in the form raising an error.

My Model models.py

from django.db import models

from django.contrib.auth.models import User

class Group(models.Model):
    group_name = models.CharField('Group name', max_length=50)

    def __str__(self):
        return self.group_name


class GroupMembers(models.Model):
    group_name = models.ManyToManyField(Group)
    members = models.ForeignKey(User, on_delete=models.CASCADE)


class Transactions(models.Model):

    bill_type = models.CharField('Bill type',max_length=200)
    added_by = models.ForeignKey(GroupMembers, on_delete=models.CASCADE)
    added_to = models.ForeignKey(Group, on_delete=models.CASCADE)
    purchase_date = models.DateField(auto_now=True)
    share_with = models.CharField('Share among',max_length=250)
    amount = models.IntegerField(default=0)

    def __str__(self):
        return self.bill_type

forms forms.py

from django import forms

from .models import Transactions, GroupMembers


class Bill_CreateForm(forms.ModelForm):
    def __init__(self, user_list, *args, **kwargs):   
        super(Bill_CreateForm, self).__init__(*args, **kwargs)
        self.fields['share_with'] = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,choices=tuple([(name, name.members) for name in user_list]))

    class Meta:
        model = Transactions
        fields = (
            'bill_type',
            'amount',
            'added_by',
            'added_to',
            'share_with',
        )

** RESOLVED MY ISSUE WITH THE HELP OF @Alasdair "

EDITED SOLUTION

views views.py

from django.shortcuts import render, redirect

from django.contrib.auth.models import User
from .models import Transactions, Group, GroupMembers
from .forms import Bill_CreateForm
from django.http import HttpResponse, HttpResponseRedirect

def add_bills_home(request, id=None):
    user = User.objects.get(pk=id) 
    grpname = Group.objects.filter(groupmembers__members=user)
    gm = GroupMembers.objects.filter(group_name__group_name=grpname[0])       
    users_list = [i for i in gm]

    if request.method == 'POST':       
        form = Bill_CreateForm(users_list, request.POST)
        if form.is_valid():
            print(form.cleaned_data['share_with'])
            form.save()

            form = Bill_CreateForm(users_list) 

            return render(request, 'bills/create_bill.html', {'form':form})
    else:
        form = Bill_CreateForm(users_list)   
    return render(request, 'bills/create_bill.html', {'form':form})

The error is

After submiting the form with data the request.POST method returning below data

i don't know if the request.POST method re-initializes the form with the filled data and pass it to the init method in the form.py.

Please help me with this

来源:https://stackoverflow.com/questions/61104612/django-forms-post-request-raising-an-error-on-init-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!