Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have only one instance of a django model

I want only one instance of the model below. How can I enforce this in code?

class Employer(models.Model):
    name=models.CharField(max_length=50, verbose_name = "Employer's Name")
    pin =models.CharField(max_length=50, verbose_name ="Employer's PIN")

    def __unicode__(self):
        return self.name
like image 756
muriithi Avatar asked Nov 14 '22 04:11

muriithi


1 Answers

try this:

class Employer(models.Model):
    name = models.CharField(max_length=50, verbose_name="Employer's Name")
    pin = models.CharField(max_length=50, verbose_name="Employer's PIN")

    def __unicode__(self):
        return self.name

    class Meta:
        unique_together = ('name', 'pin')

from Django Model Meta options.

like image 198
Vinta Avatar answered Dec 28 '22 06:12

Vinta