How to restrict for only one value

  1. after adding value using add button
  2. second time admin can edit it
  3. if value not available then can only can new value.

This is the front-end

It’s not clear what you really been asking.
Do you want to stop editing once model instance is created?

yes, I want to disable add option once a model object is created.

If you want only one model instance to be created than you can do something like


class Abc(models.Model):
    .......

    def save(self, *args, **kwargs):
        """
        Create only one Abc instance
        """
        if not self.pk and Abc.objects.exists():
            # This below line will render error by breaking page, you will see
            raise ValidationError(
                "There can be only one Abc you can not add another"
            )

            # OR you can ever return None from here, 
            # this will not save any data only you can update existing once
            return None

        return super(Abc, self).save(*args, **kwargs)
1 Like

So you want a table with only 1 record?

If yes then rethink if this data should be stored in a table.

If you have more than one of this type of situation, I’d be looking at a more fundamental restructuring of your database schema, and definitely looking at something other than the Django admin to manage it for situations like this.

@Premkant-Kori the solution that I’ve given might have worked for you but you might consider @dennisvd and @KenWhitesell point.

If you really want one object to be stored in table than you might not really needed to store it in the table unless there are other reasons, you can create a Constant dictionary and can use it wherever required or you can try other ways around as well.

1 Like

I want to make it dynamic, so that user can choose heading according to their need through admin panel.

If the heading is user dependent then a table is fine as it will have multiple records and you can let users create their own heading. For the select you can create a custom lookup. Although thinking about it, if there is only 1 choice per user there is no point in giving the user an option to select :sweat_smile:.

If it is system dependent then I would consider a different solution.