Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 750865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:36:00+00:00 2026-05-14T14:36:00+00:00

I’m currently doing a firewall management application for Django, here’s the (simplified) model :

  • 0

I’m currently doing a firewall management application for Django, here’s the (simplified) model :

class Port(models.Model):
    number = models.PositiveIntegerField(primary_key=True)
    application = models.CharField(max_length=16, blank=True)

class Rule(models.Model):
    port = models.ForeignKey(Port)
    ip_source = models.IPAddressField()
    ip_mask = models.IntegerField(validators=[MaxValueValidator(32)])
    machine = models.ForeignKey("vmm.machine")

What I would like to do, however, is to display to the user a form for entering rules, but with a very different organization than the model :

Port 80

O Not open

O Everywhere

O Specific addresses :

——— delete field

——— delete field

+ add address field

Port 443

… etc

Where Not open means that there is no rule for the given port, Everywhere means that there is only ONE rule (0.0.0.0/0) for the given port, and with specific addresses, you can add as many addresses as you want (I did this with JQuery), which will make as many rules.

Now I did a version completely “handmade”, meaning that I create the forms entirely in my templates, set input names with a prefix, and parse all the POSTed stuff in my view (which is quite painful, and means that there’s no point in using a web framework).

I also have a class which aggregates the rules together to easily pre-fill the forms with the informations “not open, everywhere, …”. I’m passing a list of those to the template, therefore it acts as an interface between my model and my “handmade” form :

class MachinePort(object):
    def __init__(self, machine, port):
        self.machine = machine
        self.port = port

    @property
    def fully_open(self):
        for rule in self.port.rule_set.filter(machine=self.machine):
            if ipaddr.IPv4Network("%s/%s" % (rule.ip_source, rule.ip_mask)) == ipaddr.IPv4Network("0.0.0.0/0"):
                return True
        else :
            return False

    @property
    def partly_open(self):
        return bool(self.port.rule_set.filter(machine=self.machine)) and not self.fully_open

    @property
    def not_open(self):
        return not self.partly_open and not self.fully_open

But all this is rather ugly ! Do anyone of you know if there is a classy way to do this ? In particular with the form… I don’t know how to have a form that can have an undefined number of fields, neither how to transform these fields into Rule objects (because all the rule fields would have to be gathered from the form), neither how to save multiple objects… Well I could try to hack into the Form class, but seems like too much work for such a special case. Is there any nice feature I’m missing ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:36:00+00:00Added an answer on May 14, 2026 at 2:36 pm

    Ok, finally I got it running by making the models closer to what I wanted to present to the user. But related to the topic of the question :

    1) Nested forms/formsets are not a built-in Django feature, are a pain to implement by yourself, and are actually not needed… Rather, one should use forms’ and formsets’ prefixes.

    2) Trying to work with forms not based on the models, process the data, then reinject it in the models, is much much more code than modifying the models a little bit to have nice model-based forms.
    So what I did is I modified the models like that :

    class PortConfig(Serializable):
        port = models.ForeignKey(Port, editable=False)
        machine = models.ForeignKey("vmm.machine", editable=False)
        is_open = models.CharField(max_length=16, default="not_open", choices=is_open_choices)
    
    class Rule(Serializable):
        ip_source = models.CharField(max_length=24)
        port_config = models.ForeignKey(PortConfig)
    

    Then I simply used a “model formset” for PortConfig, and “model inline formset” for Rule, with a PortConfig as foreign key, and it went perfectly

    3) I used this great JS library http://code.google.com/p/django-dynamic-formset/ to put the “add field” and “delete field” links … you almost have nothing to do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 380k
  • Answers 380k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's a driver bug. Your version is already 6 years… May 14, 2026 at 9:52 pm
  • Editorial Team
    Editorial Team added an answer You can return from within a try block, but keep… May 14, 2026 at 9:52 pm
  • Editorial Team
    Editorial Team added an answer I don't know what to do from inside maven, but… May 14, 2026 at 9:52 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.