How to limit the ellipsis of fields on a model?

I would like to verify that I get no more than three relationships established in a multidimensional field.

I tried the clean method for this:

if self.tags.count()>3:
  raise ValidationError(_(u'You cannot add more than 3 tags'))

But it self.tagsdoes not return current updates ... only saved objects.

Do you have an idea to access them?

thank

+3
source share
2 answers

You can do this in several ways.

You can do this first as part of saving the model ()

In your model, do something like this:

def save(self):
  # this may not be the correct check... but it will be something like this
  if self.tags.count() > 3: 
    # raise errors here
  else:
    super(MODEL_NAME,self).save()

Or you can do it manually in the view.

def some_view(request):

  # all the request.POST checking goes here

  the_model = form.save(commit=False)
  if the_model.tags.count() > 3:
    #error stuff
  else:
    the_model.save()
+3
source

. , , , , - ForeignKey ManyToMany.

-2

Source: https://habr.com/ru/post/1742994/


All Articles