Как вы создаете почтовый запрос, чтобы ModelForm был действительным (предоставляя вам все, что нужно заполнить пользователю), но затем изменяете модель (со всем, что пользователь не должен предоставлять вам [например, имя пользователя/профиль, дату и время, другие вычисляемые переменные и т.д...]) перед проверкой формы/набора форм в представлении?
Образец кода:
просмотры.py
def view(request):
# Create the formset, specifying the form and formset we want to use.
LowerFormSet = formset_factory(LowerForm, formset=BaseLowerFormSet, extra=0)
# If they are posting data
if request.method == 'POST':
# Grab the data they submitted
formData = request.POST
# highestForm = MealForm(formData, meal=meal, prefix='recipe')
lower_formset = LowerFormSet(formData, prefix='lower_formset')
lower_form in lower_formset:
lower_form.instance.a = A.objects.create(…)
# HERE
# Each form isn't valid, because it is missing its associated higher object, despite not including the field in the ModelForm
lower_form.save()
# But if we create a foreign key/higher object and add it here, we are circumventing the validation step, and creating objects when the validation might not be True
lower_form.instance.higher = Higher.objects.create(…)
lower_form.save()
# How do you separate these concerns, so you can create a valid Model Form, then add the necessary parts after the validation?
# Check for valid data
if all([otherForm.is_valid(),
lower_formset.is_valid(),
other_formset.is_valid(),
]):
# make the Higher here, and add it to each form
for form in lower_formset:
form.higher = higher
form.save()
return HttpResponseRedirect(reverse('recipe:success'))
else:
return response = render(request, 'template', {…})
else:
# …
модели.py
class Lower(models.Model):
higher = models.ForeignKey(Higher, related_name="higher")
# Etiher A or B is required, depending on information from the foreign key of the Higher
a = models.ForeignKey(A, blank=True, null=True, related_name="switch_case")
b = models.ForeignKey(B, blank=True, null=True, related_name="switch_case")
createdAt = models.DateTimeField(auto_now_add=True)
def clean(self):
if self.higher.a:
if not self.a:
raise AssertionError("The higher wants a, but you did not provide a")
if self.b:
raise AssertionError("The higher wants a, but you provided b")
else:
if not self.a:
raise AssertionError("The higher wants b, but you provided a")
if self.b:
raise AssertionError("The higher wants b, but you did not provide b")
def save(self, *args, **kwargs):
self.clean()
super(Lower, self).save(*args, **kwargs)