Django模型中的条件
我希望Django模型中的条件,django,django-models,django-forms,django-templates,django-views,Django,Django Models,Django Forms,Django Templates,Django Views,我希望company\u name在company\u被删除=False时为unique=True。类似地,当company\u被删除=True时,那么company\u name将成为unique=False。我使用软删除的地方意味着我只是设置company\u is\u deleted=True,而不是从数据库表中删除它 公司模式 class Company(models.Model): company_name = models.CharField(max_length=20, u
company\u name
在company\u被删除=False时为unique=True
。类似地,当company\u被删除=True
时,那么company\u name
将成为unique=False
。我使用软删除的地方意味着我只是设置company\u is\u deleted=True
,而不是从数据库表中删除它
公司模式
class Company(models.Model):
company_name = models.CharField(max_length=20, unique=True) # Here
company_description = models.CharField(max_length=100)
company_address = models.CharField(max_length=100)
company_email = models.EmailField()
company_website = models.URLField()
company_phone = models.CharField(max_length=30)
company_monthly_payment = models.DecimalField(max_digits=5, decimal_places=2)
company_logo = models.ImageField(upload_to='company_logo', default='default_company.png',blank=True, null=True)
company_created = models.DateTimeField(auto_now_add=True)
company_is_deleted = models.BooleanField(default=False)
View.py
class CompanyCreateView(LoginRequiredMixin, generic.CreateView):
model = Company
fields = ['company_name', 'company_description', 'company_email',
'company_website', 'company_address', 'company_phone', 'company_status',
'company_monthly_payment', 'company_logo']
您可以将该逻辑添加到save
方法中。但从公司名称
字段中删除唯一
from django.db import IntegrityError
class Company(models.Model):
company_name = models.CharField(max_length=20)
company_description = models.CharField(max_length=100)
company_address = models.CharField(max_length=100)
company_email = models.EmailField()
company_website = models.URLField()
company_phone = models.CharField(max_length=30)
company_monthly_payment = models.DecimalField(max_digits=5, decimal_places=2)
company_logo = models.ImageField(upload_to='company_logo', default='default_company.png',blank=True, null=True)
company_created = models.DateTimeField(auto_now_add=True)
company_is_deleted = models.BooleanField(default=False)
def save(self, *args, **kwargs):
if not self.company_is_deleted and Company.objects.filter(
company_name=self.company_name,
company_is_deleted=False
).exists():
raise IntegrityError
super(Company, self).save(*args, **kwargs)
您可以将该逻辑添加到save
方法中。但从公司名称
字段中删除唯一
from django.db import IntegrityError
class Company(models.Model):
company_name = models.CharField(max_length=20)
company_description = models.CharField(max_length=100)
company_address = models.CharField(max_length=100)
company_email = models.EmailField()
company_website = models.URLField()
company_phone = models.CharField(max_length=30)
company_monthly_payment = models.DecimalField(max_digits=5, decimal_places=2)
company_logo = models.ImageField(upload_to='company_logo', default='default_company.png',blank=True, null=True)
company_created = models.DateTimeField(auto_now_add=True)
company_is_deleted = models.BooleanField(default=False)
def save(self, *args, **kwargs):
if not self.company_is_deleted and Company.objects.filter(
company_name=self.company_name,
company_is_deleted=False
).exists():
raise IntegrityError
super(Company, self).save(*args, **kwargs)
不要设置unique=True
。相反,编写一个小的if…else
逻辑来获得您想要的行为。不要设置unique=True
。相反,编写一个小的if…else
逻辑来获得您想要的行为。我不想引发完整性错误,而是想引发验证错误。类似于此raise forms.ValidationError(您的电子邮件已经存在)。
您可以替代forms.py中的save
方法。因此,在访问数据库之前,会检查此条件。但我没有forms.py我使用的是generic.CreateView
而不是引发完整性错误我想引发验证错误。类似于此raise forms.ValidationError(您的电子邮件已经存在)。
您可以替代forms.py中的save
方法。因此,在访问数据库之前,会检查此条件。但我没有forms.py,我使用的是generic.CreateView