Issue
I’m using Django for a small project and I want to make a table with one row to add the description of the project in it. I only need one row and nothing more since it’s one description for the whole site and I don’t want the user to be able to add more than one description.
Solution
I solved the problem with the following code
Class Aboutus(models.Model):
....
def save(self):
# count will have all of the objects from the Aboutus model
count = Aboutus.objects.all().count()
# this will check if the variable exist so we can update the existing ones
save_permission = Aboutus.has_add_permission(self)
# if there's more than two objects it will not save them in the database
if count < 2:
super(Aboutus, self).save()
elif save_permission:
super(Aboutus, self).save()
def has_add_permission(self):
return Aboutus.objects.filter(id=self.id).exists()
Answered By – Khaled Al-Ansari
Answer Checked By – Katrina (BugsFixing Volunteer)