Answer the question
In order to leave comments, you need to log in
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)
Answer the question
In order to leave comments, you need to log in
First of all, learn python.
AttributeError: 'list' object has no attribute 'author'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question