You are not logged in.

Change Language:

Profile for alexk

Name alexk
Email Address n/a
Posts3
  • Custom tag weird behaviour
    Board » Django » Templates
    Hi all,

    I've been trying to figure this out for hours and hours, but I simply can't understand how to make it work.

    Now, we have a very simple custom template tag. Let's say that we want the template tag to take as a parameter a filtered variable. A very simple implementation of my template tag is :

    register = template.Library()
    
    class Test(template.Node):
    	def __init__(self,val):
    		self.val = val
    	def render(self,context):
    		print("VALUE=%s" % self.val)
    		if self.val.__class__.__name__ == "FilterExpression":
    			self.val = self.val.resolve(context)
    		return self.val
    
    @register.tag('test')
    def test(parser,token):
    
    	print("Test is executed")
    
    	val = token.split_contents()1
    	val = parser.compile_filter(val)
    
    	return Test(val)
    


    This just gets the value and resolves the filter. A simple template to test this is :

    {% for p in test_list %}
    	{% test p|first %} - {%test p|last%} --- {{p}}<br/>
    {% endfor %}
    


    The test_list is :
    (("First1","Last1"),("First2","Last2"),("First3","Last3"))

    So from the code I've written so far I would expect this silly template tag to iterate through and print out the first and last element of the tuples.

    Though, this is the output of the template :

    First1 - Last1 --- ('First1', 'Last1')
    First1 - Last1 --- ('First2', 'Last2')
    First1 - Last1 --- ('First3', 'Last3')

    And this is what I get in my console:

    Test is executed
    Test is executed
    VALUE=p|first
    VALUE=p|last
    VALUE=First1
    VALUE=Last1
    VALUE=First1
    VALUE=Last1

    OK, so what I understand so far is that the template player calls the test template tag the first two times. Then, somehow it creates new Test objects and feeds them with the result of the first execution.

    I am certainly not a django expert, so could someone be kind enough to explain me what on earth is this thing doing and how to get around it please ? :)
  • Re: Stored procedure on a model's custom sql
    Board » Django » Models
    i'm not sure - do you have the error message ?

    in general django ORM tries to free you from requiring to interact with the database directly .. so i don't think that stored procedures are actually supported :)
    is there no other way to implement your requirement?
    Well the error message is from mysql and is simply a syntax error. The weird part is that there is obviously no syntax error in my code, since I can load that file directly to the database.

    Yea I know what you mean about the interaction with the database, but sometimes in order to make things effective and faster we need to get our hands dirty. In this case I have the requirement that a trigger will call a stored procedure (I could include the code of the stored proc in the trigger but this is not the point). So I guess that I could implement the trigger in the python code, but it won't be as fast by any means.

    I do have the feeling that this should be supported as many people still like to do lower level stuff to make the application much more efficient and fast.

    For now I can just load the stored procs + triggers manually, and it's not really crucial for this to work, but it does save me time if I didn't have to reload the sql files each time I want to recreate the database.
  • Stored procedure on a model's custom sql
    Board » Django » Models
    Hey everyone,

    I wrote a custom stored proc which I wanted to be loaded to the database automatically on syncdb.
    So, I wrote it as custom sql code of a model. My question is if this is supported, because although I can create the procedure directly to the database, when django picks it on syncdb, mysql 5.0 raises a syntax error.

    So, I have to assume that django does something with the sql code written in my file, and that's why MySql complains ?
    If yes, any known solutions to this problem?

    Thanks for any help.


Powered by Sphene Community Tools