Condition in Model Django
i want company_name
to be unique=True
when company_is_deleted=False
. Similarly when company_is_deleted=True
then company_name
to be unique=False
. Where i am using soft delete means that i am just setting company_is_deleted=True
and not deleting it from database table.
Company Model
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']
django django-models django-forms django-templates django-views
add a comment |
i want company_name
to be unique=True
when company_is_deleted=False
. Similarly when company_is_deleted=True
then company_name
to be unique=False
. Where i am using soft delete means that i am just setting company_is_deleted=True
and not deleting it from database table.
Company Model
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']
django django-models django-forms django-templates django-views
Don't setunique=True
. Instead, write a smallif...else
logic to get the behaviour that you want.
– xyres
Jan 2 at 11:05
add a comment |
i want company_name
to be unique=True
when company_is_deleted=False
. Similarly when company_is_deleted=True
then company_name
to be unique=False
. Where i am using soft delete means that i am just setting company_is_deleted=True
and not deleting it from database table.
Company Model
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']
django django-models django-forms django-templates django-views
i want company_name
to be unique=True
when company_is_deleted=False
. Similarly when company_is_deleted=True
then company_name
to be unique=False
. Where i am using soft delete means that i am just setting company_is_deleted=True
and not deleting it from database table.
Company Model
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']
django django-models django-forms django-templates django-views
django django-models django-forms django-templates django-views
asked Jan 2 at 10:56
Huzaif SayyedHuzaif Sayyed
640119
640119
Don't setunique=True
. Instead, write a smallif...else
logic to get the behaviour that you want.
– xyres
Jan 2 at 11:05
add a comment |
Don't setunique=True
. Instead, write a smallif...else
logic to get the behaviour that you want.
– xyres
Jan 2 at 11:05
Don't set
unique=True
. Instead, write a small if...else
logic to get the behaviour that you want.– xyres
Jan 2 at 11:05
Don't set
unique=True
. Instead, write a small if...else
logic to get the behaviour that you want.– xyres
Jan 2 at 11:05
add a comment |
1 Answer
1
active
oldest
votes
You can add that logic into save
method. But remove unique
from company_name
field.
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)
instead of raising Integrity Error i want to raise Validation Error. Similar to like thisraise forms.ValidationError(u'Email already exist.')
– Huzaif Sayyed
Jan 2 at 11:15
You can overridesave
method in your forms.py instead. So before hitting database, this condition is checked.
– Reza Torkaman Ahmadi
Jan 2 at 11:21
but i dont have forms.py i am usinggeneric.CreateView
– Huzaif Sayyed
Jan 2 at 11:30
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54005084%2fcondition-in-model-django%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can add that logic into save
method. But remove unique
from company_name
field.
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)
instead of raising Integrity Error i want to raise Validation Error. Similar to like thisraise forms.ValidationError(u'Email already exist.')
– Huzaif Sayyed
Jan 2 at 11:15
You can overridesave
method in your forms.py instead. So before hitting database, this condition is checked.
– Reza Torkaman Ahmadi
Jan 2 at 11:21
but i dont have forms.py i am usinggeneric.CreateView
– Huzaif Sayyed
Jan 2 at 11:30
add a comment |
You can add that logic into save
method. But remove unique
from company_name
field.
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)
instead of raising Integrity Error i want to raise Validation Error. Similar to like thisraise forms.ValidationError(u'Email already exist.')
– Huzaif Sayyed
Jan 2 at 11:15
You can overridesave
method in your forms.py instead. So before hitting database, this condition is checked.
– Reza Torkaman Ahmadi
Jan 2 at 11:21
but i dont have forms.py i am usinggeneric.CreateView
– Huzaif Sayyed
Jan 2 at 11:30
add a comment |
You can add that logic into save
method. But remove unique
from company_name
field.
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)
You can add that logic into save
method. But remove unique
from company_name
field.
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)
edited Jan 2 at 11:32
answered Jan 2 at 11:09


Sergey PugachSergey Pugach
2,4781620
2,4781620
instead of raising Integrity Error i want to raise Validation Error. Similar to like thisraise forms.ValidationError(u'Email already exist.')
– Huzaif Sayyed
Jan 2 at 11:15
You can overridesave
method in your forms.py instead. So before hitting database, this condition is checked.
– Reza Torkaman Ahmadi
Jan 2 at 11:21
but i dont have forms.py i am usinggeneric.CreateView
– Huzaif Sayyed
Jan 2 at 11:30
add a comment |
instead of raising Integrity Error i want to raise Validation Error. Similar to like thisraise forms.ValidationError(u'Email already exist.')
– Huzaif Sayyed
Jan 2 at 11:15
You can overridesave
method in your forms.py instead. So before hitting database, this condition is checked.
– Reza Torkaman Ahmadi
Jan 2 at 11:21
but i dont have forms.py i am usinggeneric.CreateView
– Huzaif Sayyed
Jan 2 at 11:30
instead of raising Integrity Error i want to raise Validation Error. Similar to like this
raise forms.ValidationError(u'Email already exist.')
– Huzaif Sayyed
Jan 2 at 11:15
instead of raising Integrity Error i want to raise Validation Error. Similar to like this
raise forms.ValidationError(u'Email already exist.')
– Huzaif Sayyed
Jan 2 at 11:15
You can override
save
method in your forms.py instead. So before hitting database, this condition is checked.– Reza Torkaman Ahmadi
Jan 2 at 11:21
You can override
save
method in your forms.py instead. So before hitting database, this condition is checked.– Reza Torkaman Ahmadi
Jan 2 at 11:21
but i dont have forms.py i am using
generic.CreateView
– Huzaif Sayyed
Jan 2 at 11:30
but i dont have forms.py i am using
generic.CreateView
– Huzaif Sayyed
Jan 2 at 11:30
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54005084%2fcondition-in-model-django%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Don't set
unique=True
. Instead, write a smallif...else
logic to get the behaviour that you want.– xyres
Jan 2 at 11:05