gpt4 book ai didi

Django - 防止重复记录

转载 作者:行者123 更新时间:2023-12-02 07:03:03 26 4
gpt4 key购买 nike

我的数据库中有一个客户记录列表。每年,我们都会为每个客户生成一个工作订单。然后,对于每个工作订单记录,用户应该能够创建特定于该工作订单的注释。但是,并非所有工单都需要注释,只是部分工单需要注释。

现在,我无法简单地向工作订单添加 note 字段,因为有时,我们需要在生成工作订单之前创建注释。有时,此注释特定于 2-3 年内不会发生的工单。因此,注释和工作订单必须是独立的,尽管它们同时存在时会“找到”彼此。

好的,情况是这样的。我希望用户能够填写一个非常简单的 note 表单,其中有两个字段:noteYearnote。因此,他们所做的就是选择年份,然后写下注释。更关键的是,用户不应该能够为同一年的同一客户创建两个注释。

我想要得到的是通过确保该客户当年没有注释来验证注释。我假设这可以通过表单中的自定义 is_valid 方法来实现,但我不知道如何去做。

这是我到目前为止所尝试的(请注意,我知道这是错误的,它不起作用,但这是我迄今为止的尝试):

请注意,systemID 是我的客户记录

我的模型:

class su_note(models.Model):
YEAR_CHOICES = (
('2013', 2013),
('2014', 2014),
('2015', 2015),
('2016', 2016),
('2017', 2017),
('2018', 2018),
('2019', 2019),
('2020', 2020),
('2021', 2021),
('2022', 2022),
('2023', 2023),
)
noteYear = models.CharField(choices = YEAR_CHOICES, max_length = 4, verbose_name = 'Relevant Year')
systemID = models.ForeignKey(System, verbose_name = 'System ID')
note = models.TextField(verbose_name = "Note")

def __unicode__(self):
return u'%s | %s | %s' % (self.systemID.systemID, self.noteYear, self.noteType)

还有我的表格:

class SU_Note_Form(ModelForm):
class Meta:
model = su_note
fields = ('noteYear', 'noteType', 'note')
def is_valid(self):
valid = super (SU_Note_Form, self).is_valid()

#If it is not valid, we're done -- send it back to the user to correct errors
if not valid:
return valid

# now to check that there is only one record of SU for the system
sysID = self.cleaned_data['systemID']
sysID = sysID.systemID
snotes = su_note.objects.filter(noteYear = self.cleaned_data['noteYear'])
for s in snotes:
if s.systemID == self.systemID:
self._errors['Validation_Error'] = 'There is already a startup note for this year'
return False
return True

编辑 - 这是我的解决方案(感谢 janos 为我指明了正确的方向)

我的最终形式如下所示:

class SU_Note_Form(ModelForm):
class Meta:
model = su_note
fields = ('systemID', 'noteYear', 'noteType', 'note')
def clean(self):
cleaned_data = super(SU_Note_Form, self).clean()
sysID = cleaned_data['systemID']
sysID = sysID.systemID
try:
s = su_note.objects.get(noteYear = cleaned_data['noteYear'], systemID__systemID = sysID)
print(s)
self.errors['noteYear'] = "There is already a note for this year."
except:
pass
return cleaned_data

对于查看此代码的其他人来说,唯一令人困惑的部分是以下行:sysID = sysID.systemIDsystemID 实际上是另一个模型的字段 - 尽管 systemID 也是该模型的一个字段 - 设计可能很差,但它有效。

最佳答案

请参阅 Django 文档中的此页面: https://docs.djangoproject.com/en/dev/ref/forms/validation/

由于您的验证逻辑取决于两个字段(年份和系统ID),因此您需要在表单上使用自定义清理方法来实现这一点,例如:

def clean(self):
cleaned_data = super(SU_Note_Form, self).clean()
sysID = cleaned_data['systemID']
sysID = sysID.systemID
try:
su_note.objects.get(noteYear=cleaned_data['noteYear'], systemID=systemID)
raise forms.ValidationError('There is already a startup note for this year')
except su_note.DoesNotExist:
pass

# Always return the full collection of cleaned data.
return cleaned_data

关于Django - 防止重复记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15855560/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com