How do I add a table limited to only one row in mysql/django?
Asked Answered
C

4

6

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.

Callery answered 31/1, 2016 at 15:59 Comment(5)
Are you trying to achieve something similar to the sites framework?Cottonseed
@JamieBull no It's not! I was asking a question a lot couldn't answer it! stop down voting me!!!Callery
I've changed the title to reflect the question. That's probably why you were getting down-votes.Asuncion
thank you but I already solved the problem @JamieBullCallery
So now people with the same problem will be more likely to find the answer (and give you upvotes in the process).Asuncion
C
2

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()
Callery answered 2/2, 2016 at 8:34 Comment(0)
P
2

Yes this can be done. Lets say your model is MyDescModel.

class MyDescModel(models.Model):
    desc = models.CharField('description', max_length=100)

def has_add_permission(self):
    return not MyDescModel.objects.exists()

Now you can call this

MyDescModel.has_add_permission() 

before calling

MyDescModel.save()

Hope this helps.

Paulino answered 31/1, 2016 at 19:59 Comment(1)
Overriding has_add_permission is actual only for models inherited form ModelAdmin, but in this case only Model is usedRomain
C
2

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()
Callery answered 2/2, 2016 at 8:34 Comment(0)
U
0

Mar, 2022 Update:

The easiest and best way is using "has_add_permission()":

"models.py":

from django.db import models

class MyModel(models.Model):
    name = models.CharField(max_length=50)    

"admin.py":

from django.contrib import admin
from .models import MyModel

@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
    def has_add_permission(self, request): # Here
        return not MyModel.objects.exists()

You can check the documentation about has_add_permission().

Uam answered 22/3, 2022 at 11:3 Comment(0)
O
0

Just inherit yout models from SingletonBaseModel

models.py

class SingletonBaseModel(models.Model):
    def save(self, *args, **kwargs):
        if self.has_add_permission():
            log_exception(f'A record already exists in {self.__class__} model')
            raise ValueError(f'A record already exists in {self.__class__} model')
        super().save(*args, **kwargs)

    def has_add_permission(self):
        return not self.pk and self.__class__.objects.exists()

    class Meta:
        abstract = True
Omegaomelet answered 15/8, 2024 at 8:17 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.