Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django模型中的条件_Django_Django Models_Django Forms_Django Templates_Django Views - Fatal编程技术网

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