I've put together a basic CMS system for a project and I'm adding an order system so that you can order the created pages in the nav bar. Unfortunately the custom validator I've written is a little to aggressive and raises and error when you edit a page because, correctly, the order already exists in the database.
Model the form is based on:
class Page(models.Model):
page_name = models.CharField(max_length=100)
page_content = models.CharField(max_length=16777000)
link = models.URLField(blank=True)
order = models.IntegerField()
Custom Validator:
def clean_order(self):
data = self.cleaned_data['order']
pg = Page.objects.filter(order=data)
if pg.count() > 0:
raise forms.ValidationError("This order number already exists. Use another.")
return data
Is there any way for me to, upon performing the update, have the custom validator only raise an error if the new order already exists BUT where it is not current Page object. Something like:
pg = Page.objects.filter(order=data).filter(pk!=editpagepk)
Thanks!
Use the exclude method, like so:
pg = Page.objects.filter(order=data).exclude(pk=self.instance.pk)