E
E
Enter_a_nickname2022-03-29 19:17:36
Django
Enter_a_nickname, 2022-03-29 19:17:36

Why doesn't request.user work with formset?

It is necessary for me to connect the data added through forms with users, all forms fulfill correctly except for formset.
my forms.py:

class ColorForms(ModelForm):
    class Meta:
        model = Color
        fields = ['temp', 'correct', 'top', 'bottom']
        widgets = {
            'temp': NumberInput(attrs={
                'class': 'form-control',
                'placeholder': 'temp'
            }),
            'correct': NullBooleanSelect(attrs={
                'class': 'form-control',
                'placeholder': 'correct'
            }),
            'top': NumberInput(attrs={
                'class': 'form-control',
                'placeholder': 'top'
            }),
            'bottom': NumberInput(attrs={
                'class': 'form-control',
                'placeholder': 'bottom'
            }),
        }

    class SpeedsForms(ModelForm):
    class Meta:
        model = Speed
        fields = ['start',  'end']
        widgets = {
            "start": TimeInput(attrs={
                'class': 'form-control',
                'placeholder': "start"
            }),
            "end": TimeInput(attrs={
                'class': 'form-control',
                'placeholder': "end"
            })
        }

    DogFormSet = modelformset_factory(
    Dog, fields=('name', 'number', 'Speed', 'DataTime'), extra=1
)

    class DogRequestsForm(ModelForm):
    class Meta:
        model = DogRequest
        fields = ['id_name', 'Color', 'Dog']
        widgets = {
            'id_name': NumberInput(attrs={
                'class': 'form-control',
                'placeholder': 'id_name'
            }),
            'Color': SelectMultiple(attrs={
                'class': 'form-control',
                'placeholder': 'Color'
            }),
            'Dog': SelectMultiple(attrs={
                'class': 'form-control',
                'placeholder': 'Dog'
            }),
        }


from .forms import DogFormSet 

    def dog(request):
    formset = DogFormSet (queryset=dog.objects.none())

    if request.method == 'POST' and 'speed' in request.POST:
        form2 = SpeedsForms(request.POST)
        if form2.is_valid():
            post = form2.save(commit=False)
            post.author = request.user
            post.save()

    if request.method == 'POST' and 'dogs' in request.POST:
        formset = DogFormSet(request.POST)
        if formset.is_valid():
            post = formset.save(commit=False)
            post.author = request.user
            post.save()

    if request.method == 'POST' and 'color' in request.POST:
        form4 = ColorForms(request.POST)
        if form4.is_valid():
            post = form4.save(commit=False)
            post.author = request.user
            post.save()

    if request.method == 'POST' and 'Req' in request.POST:
        form5 = DogRequestsForm(request.POST)
        if form5.is_valid():
            post = form5.save(commit=False)
            post.author = request.user
            post.save()

    form2 = SpeedsForms()
    form4 = ColorForms()
    form5 = DogRequestsForm()
    data = {
        'form2': form2,
        'formset': formset,
        'form4': form4,
        'form5': form5,
    }

    return render(request, 'main/dogsform.html', data)


When adding data via a form (key 'formset') I get an error:
"post.author = request.user
AttributeError: 'list' object has no attribute 'author'" with a link to the line post.author = request.user in the formset.
Moreover, if I add this attribute to
"DogFormSet = modelformset_factory(
Dog, fields=('author', 'name', 'number', 'Speed', 'DataTime'), extra=1)"
the error disappears, but the entry in bd is not happening. Django is returning the code "200 7978" to me.
What needs to be corrected so that when filling out this form, everything works correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Nesterov, 2022-03-29
@Enter_a_nickname

First of all, learn python.

AttributeError: 'list' object has no attribute 'author'

It explicitly says that the "list" type object does not have an "author" attribute. If you were versed in python, then even without paying attention to the name "modelformset_factory" (which, as it were, already hints that set returns), that save() returned not an instance of the model with which you can work, but the list itself . which needs to be iterated , at least.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question