N
N
nurzhannogerbek2017-05-13 21:27:57
Layout
nurzhannogerbek, 2017-05-13 21:27:57

Saving data in ManyToManyField?

Hello! Help me to understand.
There is a Requirement model with two fields (function, program) of the ManyToManyField data type . For the function field in forms I used ModelChoiceField , and for the program field I used ModelMultipleChoiceField . In other words, the user can add one function and several programs through the form. Unable to save form data. What am I missing and why is the save_m2m() method not working?
modals.py:

class Requirement(models.Model):
    group_requirement = models.ForeignKey(GroupRequirement, on_delete=models.CASCADE)

    function = models.ManyToManyField("Function")
    program = models.ManyToManyField('Program')

forms.py:
class RequirementForm(forms.ModelForm):
    function = forms.ModelChoiceField(widget=Select2Widget(), queryset=Function.objects.none())
    program = forms.ModelMultipleChoiceField(widget=Select2MultipleWidget(), queryset=Program.objects.none())

    class Meta:
        model = Requirement
        fields = ('function', 'program')

    def __init__(self, all_functions, all_programs, *args, **kwargs):
        super(RequirementForm, self).__init__(*args, **kwargs)
        self.fields['function'].queryset = all_functions
        self.fields['program'].queryset = all_programs

views.py:
def requirement_add(request, project_id, group_requirement_id):
    group_requirement = get_object_or_404(GroupRequirement, pk=group_requirement_id)

    all_functions = Function.objects.filter(project=project_id)
    all_programs = Program.objects.filter(project=project_id)

    if request.method == 'POST':
        requirement_form = RequirementForm(data=request.POST, all_functions=all_functions, all_programs=all_programs)
        if requirement_form.is_valid():
           requirement = requirement_form.save(commit=False)
           requirement.group_requirement = group_requirement
           requirement.save()
           requirement_form.save_m2m()

ERROR:
Traceback (most recent call last):
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\exception.py", line 39, in inner
    response = get_response(request)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Nurzhan\PycharmProjects\RMS\project\views.py", line 1397, in requirement_add
    requirement_form.save_m2m()
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\forms\models.py", line 436, in _save_m2m
    f.save_form_data(self.instance, cleaned_data[f.name])
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\fields\related.py", line 1573, in save_form_data
    getattr(instance, self.attname).set(data)
  File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\fields\related_descriptors.py", line 928, in set
    objs = tuple(objs)
TypeError: 'Function' object is not iterable

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Arkady Baganin, 2018-07-19
@ark_yt

Maybe the hosting does not support sending emails. See if "Mailer" is there, try smpt!!!

M
Maxim Timofeev, 2018-07-19
@webinar

tell me what to do...

watch logs. "Oh, something hurts" is not treated. Try knocking on wood.

D
Doc44, 2018-07-19
@Doc44

View email sending logs.
If they are not there, include (or make) a record.

D
Dmitry Burnaev, 2017-05-13
@nurzhannogerbek

At the time of attempting to save many to many, the method waits for exactly the object to be interposed (a querieset or a list of objects of the 'Function' type). use a different widget (or modify this one) or use a ForeignKey ... If the logic is such that only 1 element needs to be selected ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question