Question

I have an application having a model called Verifications. It has fields like: asset_code, Status, Location, Emp_id etc. I can add verifications using "Add Verification" from django admin panel. But I want to restrict adding duplicate asset_code entries (if asset_code already exists).

class Verification(models.Model):
    asset_code = models.CharField(verbose_name="Asset Code",max_length=16, default="")
    scan_time = models.DateTimeField(verbose_name="Time of smartDNA scan",auto_now_add=True,default=datetime.datetime.now)
    credential = models.CharField(verbose_name="smartDNA Credential",max_length=16, default="")
    status = models.IntegerField(verbose_name="Scanning Status",choices=STATUS_CHOICES,default=1)
    operator = models.CharField(verbose_name="Operator",max_length=16, default="")
    location = models.CharField(verbose_name="Branch",max_length=64, default="")
    auth_code = models.CharField(verbose_name="Scanner Authentication",max_length=20, default="DSC(Verisign")
    emp_id = models.CharField(verbose_name="EMP ID",max_length=16, default="")
    image = models.CharField(verbose_name="Image",max_length=24, default="dd")
    created = models.DateTimeField(verbose_name="Created on",blank=True,auto_now_add=True)
    modified = models.DateTimeField(verbose_name="Modified on",blank=True,auto_now=True)
    product_details = models.CharField(verbose_name="Product Details",max_length=64, default="")

Status field can have values between 1 to 10.

How can I prevent add entries in model if asset_code already exist and having status=1.

Was it helpful?

Solution

add unique=True to asset_code field definition

UPDATE:

Only for admin:

admin.py

from django import forms

class VerificationAdminForm(forms.ModelForm):
    class Meta:
        model = Verification

    def clean_asset_code(self):
        asset_code = self.cleaned_data['asset_code']
        if Verification.objects.filter(asset_code=asset_code).exists():
            raise forms.ValidationError("This asset code already exist.")
        return asset_code

class VerificationAdmin(admin.ModelAdmin):
    form = VerificationAdminForm

OTHER TIPS

Do you mean having two or more instances with same asset_code AND status is ok as long as only one has status=1 ? Or that the combination of asset_code and status should be unique ? If the second, it's quite simple:

class Verification(models.Model):
    # your fields here 
    class Meta:
        unique_together = [("asset_code", "status)]

The first case (which would be a rather strange requirement but anyway...) is more involved, you either need a trigger in the database or some custom validation in the model, cf https://docs.djangoproject.com/en/1.6/ref/models/instances/#django.db.models.Model.validate_unique

Oh and yes: if you only want to prevent this in the admin, you should instead provide your own ModelForm for the ModelAdmin and write your validation in the form's clean method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top