0

I have a django modelForm containing these fields:

nod_id1 = models.ForeignKey('Eonodes',..)
nod_id2 = models.ForeignKey('Eonodes',..)

that are rendered like dropdown lists as expected.

I want to do the following: when a user selects an option from both lists a python script need to be executed that calls a db function with arguments the two options of the user. What the function returns I want to be saved to an other field of the form (directiondb = models.IntegerField()). the script could be sth like this:

cursor = connection.cursor()
cursor.execute("SELECT GETDIRECTIONDB(262,265) from sys.dual")
result = cursor.fetchall()

where 262,265 are the user choices.

How can I accomplish it on the fly, I mean before data are submitted?

1
  • Your first line mentions "a django modelForm" - I take it that should read "a django model" (a model form is a form that is generated from a model. Commented Jul 17, 2012 at 11:23

1 Answer 1

1

override your model's save method to do custom actions before the data is saved. https://docs.djangoproject.com/en/dev/topics/db/models/#overriding-model-methods

def get_direction_db(self.nod_id1, self.nod_id2):
    cursor = connection.cursor()
    cursor.execute("SELECT GETDIRECTIONDB(262,265) from sys.dual")
    result = cursor.fetchall()
    return result[0]  # or whatever

class MyModel(models.Model):
    nod_id1 = models.ForeignKey('Eonodes',..)
    nod_id2 = models.ForeignKey('Eonodes',..)
    directiondb = models.IntegerField()

    def save(self, *args, **kwargs):
        self.directiondb = get_direction_db(self.nod_id1, self.nod_id2)
        super(MyModel, self).save(*args, **kwargs)
Sign up to request clarification or add additional context in comments.

4 Comments

awesome! Do u know maybe if I didn't have the field directiondb and I just wanted the returned value from get_direction_db to be displayed on the form after the user-node-choices, how I could do it?
so the user chooses from the dropdown lists and then the form is dynamically updated with the value from get_driection_db() ? What you need to do is use JavaScript to add event handlers to the drop down lists. The event handler will use an AJAX call to pass the node values back to your web app and will update the form with the result. You most likely want to use jQuery.
Yes,exactly. I just don't sense how I will do this since my dropdown lists are rendered through foreignKey-fields in modelFroms and not through the typical Select/html etc.
the modelForm just generates a regular HTML form with regular HTML select elements. Look at the HTML source of the rendered page - it's easy. I'd recommend using jQuery to manipulate the form. Do this in your browser's JS console so you can quickly experiment and learn.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.