在保存到Models.py之前对ManyToManyField进行验证 [英] Validation on ManyToManyField before Save in Models.py

查看:89
本文介绍了在保存到Models.py之前对ManyToManyField进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下模型:

class Application(models.Model):
 users = models.ManyToManyField(User, through='Permission')
 folder = models.ForeignKey(Folder)

class Folder(models.Model):
 company = models.ManyToManyField(Compnay)

class UserProfile(models.Model):
 user = models.OneToOneField(User, related_name='profile')
 company = models.ManyToManyField(Company)

我想做的是检查应用程序的一个用户是否与应用程序具有相同的公司(通过夹)。如果是这种情况,则应该不会保存应用程序实例。

What I would like to do is to check whether one of the users of the Application has the same company as the Application (via Folder). If this is the case the Application instance should not be saved.

问题是,在保存后信号之后,ManyToManyField不会更新。

唯一的选项似乎是新的m2m_changed信号。但是我不知道如何回滚已经发生的保存。

另一个选项是重写保存函数(在models.py中,因为我在谈论管理员这里),但我不知道如何访问manytomanyfield内容。

最后我读了一些关于在admin.py中的模型管理员中重写保存功能的内容,但是我仍然会不知道你将如何访问manytomanyfield内容。

The problem is that the ManyToManyFields aren't updated until after the 'post-save' signal.
The only option seems to be the new m2m_changed signal. But I'm not sure how I then roll back the save that has already happened.
Another option would be to rewrite the save function (in models.py, because I'm talking about the admin here), but I'm not sure how I could access the manytomanyfield content.
Finally I've read something about rewriting the save function in the admin of the model in admin.py, however I still wouldn't know how you would access the manytomanyfield content.

我一直在寻找这个无处不在,但没有什么我遇到似乎为我工作。

如果有什么不清楚,请告诉我。

I have been searching for this everywhere but nothing I come across seems to work for me.
If anything is unclear, please tell me.

感谢您的帮助!

Heleen

Thanks for your help!
Heleen

推荐答案

由于我没有得到Botondus的回复,我决定在 Django Users Google Group ,最后得到了jaymz的答案。

Because I didn't get a reply from Botondus I decided to ask a new question in the Django Users Google Group and finally got the answer from jaymz.

我认为Botondus方法是正确的方法,它只是不能正常工作。在这种情况下它不起作用的原因是因为我正在使用通过模型来进行验证。由于一些早期的反馈,我收到了一个先前发布的问题,我收集了第一个应用程序实例被保存,然后ManyToMany实例被保存(我相信这是正确的,但如果我错了,纠正我)。所以我认为,如果我将在Through模型中的ManyToMany字段上执行验证,这不会阻止应用程序实例被保存。但实际上它确实阻止了这种情况发生。

I figured that Botondus method was the right way of doing it, it just wasn't quite working. The reason that it doesn't work in this case is because I'm using a Through model for the field I would like to do the validation on. Because of some earlier feedback I got on a previously posted question I gathered that first the Application instance is saved and then the ManyToMany instances are saved (I believe this is right, but correct me if I'm wrong). So I thought that, if I would perform the validation on the ManyToMany Field in the Through model, this would not prevent the Application instance being saved. But in fact it does prevent that from happening.

所以如果您的模型管理员中有一个ManyToMany字段,并且您想在该字段上进行验证,则可以在直通模型中指定干净的函数,就像这样:

$ b

So if you have a ManyToMany Field inline in your model's admin and you would like to do validation on that field, you specify the clean function in the through model, like this:

admin.py
class PermissionInline(admin.TabularInline):
    form = PermissionForm
    model = Permission
    extra = 3

forms.py
class PermissionForm(forms.ModelForm):
    class Meta:
        model = Permission

    def clean(self):
        cleaned_data = self.cleaned_data
        user = cleaned_data['user']
        role = cleaned_data['role']
        if role.id != 1:
            folder = cleaned_data['application'].folder
            if len(filter(lambda x:x in user.profile.company.all(),folder.company.all())) > 0: # this is an intersection
                raise forms.ValidationError("One of the users of this Application works for one of the Repository's organisations!")
        return cleaned_data 

如果验证导致错误NOTHING(既不是应用程序实例也不是manytomany用户实例),并且您有机会更正错误。

If the validation results in an error NOTHING (neither the application instance, nor the manytomany users instances) is saved and you get the chance to correct the error.

这篇关于在保存到Models.py之前对ManyToManyField进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆