inlineformset_factory仅保存最后一行数据?

时间:2020-07-20 17:36:00

标签: django inline-formset

已保存

父模型数据,但表单集数据仅存储最后行信息。 找不到问题。这里给出所有相关代码。找不到问题。

我正在使用jquery.formset.js。

这是我的模特: 我使用两个Meal和UserMeal模型创建inlineformset_factory。

struct PrintHTMLView: UIViewControllerRepresentable
{
    let title: String
    let formatter: UIMarkupTextPrintFormatter
    let callback: () -> ()

    init(htmlString: String, title: String, completion: @escaping () -> () = {}) {
        self.title = title
        self.callback = completion

        formatter = UIMarkupTextPrintFormatter(markupText: htmlString)
        formatter.perPageContentInsets = UIEdgeInsets(top: 72, left: 72, bottom: 72, right: 72)
    }

    func makeUIViewController(context: Context) -> UIViewController
    {
        let printInfo = UIPrintInfo(dictionary: nil)
        printInfo.jobName = title
        printInfo.outputType = .general

        let printController = UIPrintInteractionController.shared
        printController.printInfo = printInfo
        printController.showsNumberOfCopies = true

        printController.printFormatter = formatter

        let controller = UIViewController()
        DispatchQueue.main.async {
            printController.present(animated: true, completionHandler: { _, _, _ in
                printController.printFormatter = nil
                self.callback()
            })
        }
        return controller
    }

    func updateUIViewController(_ uiViewController: UIViewController, context: Context)
    {
    }
}

struct TestPrintController: View {
    @State private var printShowing = false
    var body: some View {
        Button("Print")
        {
            self.printShowing = true
        }
        .background(Group {
            if self.printShowing {
                PrintHTMLView(htmlString: "Test String", title: "Report") {
                    self.printShowing = false
                }
            }
        })

    }
}

这是我的表格。

class Meal(models.Model):
    mess = models.ForeignKey(
        Mess, on_delete=models.CASCADE, related_name='mess_meals')
    active = models.BooleanField(default=True)
    meal_date = models.DateField(default=datetime.date.today)

class UserMeal(models.Model):
    meal = models.ForeignKey(
        Meal, on_delete=models.CASCADE, related_name='meals')
    user = models.ForeignKey(
        get_user_model(), on_delete=models.CASCADE, related_name='user_meals')
    morning = models.FloatField(
        validators=[MinValueValidator(0.0)], default=0.0)
    noon = models.FloatField(validators=[MinValueValidator(0.0)], default=0.0)
    night = models.FloatField(validators=[MinValueValidator(0.0)], default=0.0)
     

这是我的Views.py

class PreviousMealForm(ModelForm):
    class Meta:
        model = Meal
        exclude = ('mess',)
        widgets = {
            'meal_date': DateInput(),
        }

    def clean_meal_date(self):
        meal_date = self.cleaned_data.get('meal_date')
        check_meal = Meal.objects.filter(
            mess=self.mess_obj, meal_date=meal_date).first()
        if check_meal:
            raise forms.ValidationError(
                f"Meal Was Created For {meal_date}")
        return meal_date

    def __init__(self, *args, **kwargs):
        self.mess_obj = kwargs.pop('mess_obj', None)
        super(PreviousMealForm, self).__init__(*args, **kwargs)


class AddPreviousUserMealForm(ModelForm):
    class Meta:
        model = UserMeal
        fields = '__all__'

    def __init__(self, *args, **kwargs):
        self.mess_member = kwargs.pop('mess_member', None)
        super(AddPreviousUserMealForm, self).__init__(*args, **kwargs)
        self.fields['user'] = forms.ModelChoiceField(
            queryset=get_user_model().objects.filter(id__in=self.mess_member))


AddPreviousUserMealFormSet = inlineformset_factory(
    Meal, UserMeal, form=AddPreviousUserMealForm, extra=0, min_num=1, validate_min=True)

0 个答案:

没有答案