使用外键的django自定义表单验证

时间:2022-04-03 07:11:12

I have the following models:

我有以下模型:

class Computer(models.Model):
    ...

class Demo(models.Model):
    computers = models.ManyToManyField(Computer)
    ...

class Scenario(models.Model):
    demo = models.ForeignKey(Demo)
    ...

class Setting(models.Model):
    scenario = models.ForeignKey(Scenario)
    computer = models.ForeignKey(Computer)

Basically a Demo uses multiple computers. A demo also has multiple scenarios. Each scenario has some settings and each setting configures a computer.

基本上,演示程序使用多台计算机。演示程序也有多个场景。每个场景都有一些设置,每个设置配置一台计算机。

My problem is while using the django Admin site to add a scenario, after the user selects a demo in the drop down list and configures the settings for some computers, I need to validate that the computers in the settings are actually in the demo.

我的问题是,在使用django管理站点添加场景时,用户在下拉列表中选择一个demo并配置一些计算机的设置后,我需要验证设置中的计算机实际上是在demo中。

I've poured through the django documentation, online sites, and tried everything I can think of and still can't get this work.

我已经通过了django文档,在线站点,尝试了所有我能想到的并且仍然不能得到这个工作的东西。

I can't use the custom form validation because although I could get the 'demo' object from the cleaned_data in the scenario form, I can't seem to get access to the settings that get submitted with the form. If I do model level validation by overriding 'clean', that only works when I change a scenario not when I add a new one because the computer_set is empty for new ones.

我不能使用自定义表单验证,因为尽管我可以从场景表单的cleaned_data中获得“demo”对象,但我似乎无法访问表单提交的设置。如果我通过重写“clean”来进行模型级别验证,这只在更改场景时有效,而不是在添加新场景时有效,因为对于新场景,computer_set是空的。

Any help is greatly appreciated.

非常感谢您的帮助。

1 个解决方案

#1


2  

You could just add a custom form to your SettingInline (I'm assuming from your post that Setting is an inline for Scenario).

您可以向您的SettingInline添加一个自定义表单(我假设您的文章中设置是一个内联的场景)。

You mention you can't use form validation but I don't see a reason why you'd need access to every other setting. If you want access to the other settings (say validation that involves all submitted settings) I'd override the formset itself.

您提到不能使用窗体验证,但我看不出为什么需要访问所有其他设置。如果您希望访问其他设置(比如涉及所有提交设置的验证),我将覆盖formset本身。

class SettingForm(forms.ModelForm):
    class Meta:
        model = Setting

    def clean_computer(self):
        computer = self.cleaned_data.get('computer')
        if not self.instance.scenario.demo.computers.filter(computer=computer).count():
            raise forms.ValidationError("Computer not in demo")
        return computer

class SettingInline(admin.TabularInline):
    model = Setting
    form = SettingForm

#1


2  

You could just add a custom form to your SettingInline (I'm assuming from your post that Setting is an inline for Scenario).

您可以向您的SettingInline添加一个自定义表单(我假设您的文章中设置是一个内联的场景)。

You mention you can't use form validation but I don't see a reason why you'd need access to every other setting. If you want access to the other settings (say validation that involves all submitted settings) I'd override the formset itself.

您提到不能使用窗体验证,但我看不出为什么需要访问所有其他设置。如果您希望访问其他设置(比如涉及所有提交设置的验证),我将覆盖formset本身。

class SettingForm(forms.ModelForm):
    class Meta:
        model = Setting

    def clean_computer(self):
        computer = self.cleaned_data.get('computer')
        if not self.instance.scenario.demo.computers.filter(computer=computer).count():
            raise forms.ValidationError("Computer not in demo")
        return computer

class SettingInline(admin.TabularInline):
    model = Setting
    form = SettingForm