# Is it possible to do the following?

**URL:** <https://forum.djangoproject.com/t/is-it-possible-to-do-the-following/9591>\
**Category:** Using Django\
**Created:** [September 7, 2021, 2:41am UTC](https://forum.djangoproject.com/t/is-it-possible-to-do-the-following/9591 "2021-09-07T02:41:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![CalvinTheCat](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/calvinthecat/32/5343_2.png) [@CalvinTheCat](https://forum.djangoproject.com/u/CalvinTheCat)\
**Post date:** [September 7, 2021, 2:41am UTC](https://forum.djangoproject.com/t/is-it-possible-to-do-the-following/9591/1 "2021-09-07T02:41:16Z")

</div>

I have a formset which have 10 forms. In each form, there are 3 fields. moduletype, firstportid, lastportid. Each id for the forms are deviceinterface-set(form number)-field. In my case, user must key in the following format for firstportid and lastportid : int/int/int. Example: 1/0/1 for firstportid and 1/0/5 for lastportid. But the 1st int and the middle int can be 1-18 & 1-4. The last int can be between 1 to 48. Is there a fast way to check the formset for the following instead of getting the values from the field one by one to check.

An example: In the formset i filled up 2 out of the 10 forms. For the 1st form, my values are 5/0/1 (firstportid) and 5/0/5 (lastportid). For the 2nd form, it will be 4/2/1 (firstportid) and 4/2/10 (lastportid). Instead of getting the values one by one from form and then the field in the form. Is there some way for me to loop thru to get the values fast? For now i can only think of getting the values one by one from each of the form. Such as breaking down the values to extract a single number and do what i need

---

<div class="post-metadata">

**Author:** ![Rigo-Villalta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rigo-villalta/32/5667_2.png) [@Rigo-Villalta](https://forum.djangoproject.com/u/Rigo-Villalta)\
**Post date:** [September 7, 2021, 3:19pm UTC](https://forum.djangoproject.com/t/is-it-possible-to-do-the-following/9591/2 "2021-09-07T15:19:45Z")

</div>

I am not completely sure I understand what do you want to do, but i think you should try to accomplish a first verification at FrontEnd level with Javascript.

know if you want to check the fields without create a manual loop in the form validation you can add [validators](https://docs.djangoproject.com/en/3.2/ref/validators/) to your models or your forms. IMHO for what you want to achieve you should use the built-in [RegexValidator](https://docs.djangoproject.com/en/3.2/ref/validators/#regexvalidator) in the fields moduletype, firstportid, lastportid of your model , for example I coded this for a project:

```auto
from django.core.validators import RegexValidator
#...

class MyModel(models.Model):
   # ...
   contact_phone_number = models.CharField(
        verbose_name=_("contact_phone_number"),
        max_length=15,
        help_text=_(
            "Provide a phone number from El Salvador in order to contact on emergencies."
        ),
        validators=[
            RegexValidator(
                "^\(503\)\s[267]\d{3}-\d{4}$",
                _("This is not a valid phone number for El Salvador."),
            )
        ],
        null=True,
    )
   # ...

```

In your case you max\_length should be 7 and your regular expression must be adapted to the desired format.

---

<div class="post-metadata">

**Author:** ![CalvinTheCat](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/calvinthecat/32/5343_2.png) [@CalvinTheCat](https://forum.djangoproject.com/u/CalvinTheCat)\
**Post date:** [September 8, 2021, 6:04am UTC](https://forum.djangoproject.com/t/is-it-possible-to-do-the-following/9591/3 "2021-09-08T06:04:15Z")

</div>

Thank you very much. I did the logic check in views instead, if else loop. Is it a good practice to do it there?

---

<div class="post-metadata">

**Author:** ![Rigo-Villalta](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/rigo-villalta/32/5667_2.png) [@Rigo-Villalta](https://forum.djangoproject.com/u/Rigo-Villalta)\
**Post date:** [September 8, 2021, 7:25pm UTC](https://forum.djangoproject.com/t/is-it-possible-to-do-the-following/9591/4 "2021-09-08T19:25:15Z")

</div>

No, in fact is consider a bad practice.  
See this:

[https://sunscrapers.com/blog/where-to-put-business-logic-django/](https://sunscrapers.com/blog/where-to-put-business-logic-django/)

That is not a dogma, but IMHO you should try to follow the next order in logic of your apps in a wider perspective:Managers, custom model fields, helpers functions(as validators), models, mixins or decorators, forms, views and finally templates.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [September 8, 2021, 8:35pm UTC](https://forum.djangoproject.com/t/is-it-possible-to-do-the-following/9591/5 "2021-09-08T20:35:05Z")

</div>

The view’s ok, but putting it in validators within the form may be more appropriate. That way you can reject an invalid form entry from within the context of the form-handling view.

---

<div class="post-metadata">

**Author:** ![CalvinTheCat](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/calvinthecat/32/5343_2.png) [@CalvinTheCat](https://forum.djangoproject.com/u/CalvinTheCat)\
**Post date:** [September 9, 2021, 6:34am UTC](https://forum.djangoproject.com/t/is-it-possible-to-do-the-following/9591/6 "2021-09-09T06:34:26Z")

</div>

Oh okay. Thanks for answering 🙂

---

<div class="post-metadata">

**Author:** ![CalvinTheCat](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/calvinthecat/32/5343_2.png) [@CalvinTheCat](https://forum.djangoproject.com/u/CalvinTheCat)\
**Post date:** [September 9, 2021, 6:34am UTC](https://forum.djangoproject.com/t/is-it-possible-to-do-the-following/9591/7 "2021-09-09T06:34:58Z")

</div>

Thank you very much 🙂
