Django模型覆盖保存/使用信号/或使用模型窗体?
【腾讯云】亏本大甩卖,服务器4核16G 1年370元(带宽12M,系统盘120GB SSD盘,月流量2000GB)!!!!!!
云产品 配置 价格
服务器 1核2G,带宽5M,系统盘50GB SSD盘,月流量500GB 38元/年
MySQL 1核1G 19元/年
服务器 16核32G,带宽18M,系统盘250GB SSD盘,月流量5000GB 1197元/年
点我进入腾讯云,查看更多详情

I realize this has been asked before, but I wasn't able to find a question that really dealt with what I'm trying to do. I think it's pretty simple, but I'd like to know what the general population thinks is best form here.

Lets say we have the following:

models.py

class TestClass(models.Model):
    user = models.ForeignKey(User)
    testfield = models.CharField()
    testbool = models.BooleanField(default=False)

def save(self, *args, **kwargs):
    """
    - what we're trying to do here is ensure that the User doesn't have more than
      X (lets say 5) related test fields.
    - what if we also wanted to add validation to testfield to ensure it was 
      only [a-zA-Z]?
    """
    if TestClass.objects.filter(user=self.user).count() >= 5:
        # How do we exit gracefully?
        return 
    super(TestClass, self).save(*args, **kwargs)

The comments in the save function pretty much sum up my question: - How would we ensure that there aren't more than 5 related TestClass's to the giving user - How do we exit gracefully from save (without saving) if there are already 5 - How do we report this back to the user? - where do we validate the testfield object to ensure it only has [a-z]? can I just import re and do that here as well? should i?

Is it best to throw this all in here? Should I fire off a pre_save signal? Or should I just use a ModelForm w/ validation?

#0

You're right that there's currently no obvious place to do this. That's why there's a Summer of Code project going on right now to add model-level validation, which should make this sort of thing nice and easy.

Unfortunately it won't be ready for a couple of months. In the meantime, your answer depends on how data is being added. Will it ever be done programatically, or will it always be via a form? If the latter, then putting the validation into the form is the way to go (note that you can use the same modelform, or subclasses of it, in both the admin and your own view, so no need to duplicate logic).

推荐文章

我应该如何在springmvc中将模型bean映射到视图模型bean?

我应该如何在springmvc中将模型bean映射到视图模型bean?

推荐文章

Android菜单/对话框出现在所选列表项上方

Android菜单/对话框出现在所选列表项上方

推荐文章

如何获取Facebook封面相册的照片ID?

如何获取Facebook封面相册的照片ID?

推荐文章

如何在CEdit控件中自动选择所有文本

如何在CEdit控件中自动选择所有文本

推荐文章

为什么我不能在Cocos2d/Objective c中使用这个sprite?

为什么我不能在Cocos2d/Objective c中使用这个sprite?

推荐文章

闪存套接字策略文件要求

闪存套接字策略文件要求

推荐文章

如果公式发生变化,是否可以阻止计算列更改其值?

如果公式发生变化,是否可以阻止计算列更改其值?

推荐文章

在()中针对Oracle Many或vs的SQL性能调整

在()中针对Oracle Many或vs的SQL性能调整

推荐文章

C中与free()相关的查询

C中与free()相关的查询

推荐文章

这在js“xin | |{}中有什么作用?

这在js“xin | |{}中有什么作用?

推荐文章

谷歌分析跟踪动态生成的PDF

谷歌分析跟踪动态生成的PDF

推荐文章

Ms Access作为后端asp.net?

Ms Access作为后端asp.net?

推荐文章

自定义手风琴禁用jquery中的单击

自定义手风琴禁用jquery中的单击

推荐文章

实施主干网.Subset.js在主干网.js从父集合筛选模型的步骤

实施主干网.Subset.js在主干网.js从父集合筛选模型的步骤

推荐文章

具有不同数据类型的计算列

具有不同数据类型的计算列

推荐文章

网站使IE崩溃,需要重新启动(!)

网站使IE崩溃,需要重新启动(!)