doing queries in django templates

Well it seems that you can’t do ‘generic’ queires from within a template — the only reason being as I can tell– that functions don’t take parameters when you call them from within a tempalte. Pity.

Therefore the way to query within a template is to make a ‘helper function’ that will do your query for you, if possible.

I think. According to this post
* Attribute access of a related object which hadn’t previously been
accessed (and select_related will work around that for you so you get
all the queries up-front).
* Method accesses which fetch data (these should be documented in your models).
* Template tags which execute SQL (these should be documented in the
template tags which are executing the SQL).

So just build a helper function and hope it helps 🙂

how to display forms only data

So say you want to just display a form — plaster it on the screen, without any boxes?

Add this to django/newforms/forms.py

    def as_data_only(self): # lodor tell world, django :)
        if not self.form.is_bound:
            data = self.form.initial.get(self.name, self.field.initial)
        else:
            data = self.data
        return data

and in the view template:

{% for field in userForm %}
{{ field.label }}:
{{ field.as_data_only }}
{% if field.help_text %}<dd>{{ field.help_text }}</dd>{% endif %}
{% if field.errors %}<dd class=”myerrors”>{{ field.errors }}</dd>{% endif %}
<br>
{% endfor %}

How to save with a newform into the DB with missing parameters

As django auto forms seem to not do anything except ‘many to many’ relationships yet (yeck) and, with foreign keys you probably don’t want the visitor just setting those anyway, you are left to use an autoform but ‘fill in’ bits and pieces of this ‘partial’ autoform for the user before saving it.

One way to do it is this (uses ‘editable=false’) to not display certain (necessary) things for the user


class ResponseToEvaluation(models.Model):
originating_evaluation = models.ForeignKey(Evaluation,editable=False) # lodo umm...foreginkey then just doesn't work with these things?
personal_comments = models.CharField(maxlength=2000)
pub_date = models.DateTimeField(editable=False)

ResponseToEvaluationAutoForm = form_for_model(ResponseToEvaluation)

Then in the view thus:

from django.newforms.models import * # save_instance
def comment_reply_view(request, id):
        if request.method == 'POST':
            form = ResponseToEvaluationAutoForm(request.POST)
            rNew = ResponseToEvaluation()
            rNew.originating_evaluation_id = id # muhaha
            rNew.pub_date = datetime.now() # we want that one, too
            if form.is_valid():
                save_instance(form, rNew) # stolen from modeltests/model_forms/models.py yeck
                return returnUserHomePage(request, extraHash = {'display_message': 'Response written'})
            else:
                return returnUserHomePage(request, extraHash = {'error_message': 'Trouble writing response'})
        else:
            form = ResponseToEvaluationAutoForm()
        originalEvaluation = Evaluation.objects.get(pk=id)
        repliesToIt = originalEvaluation.responsetoevaluation_set.order_by('pub_date')
        return render_to_response('user/comment_reply_to.html', {'form': form, 'originalEvaluation': originalEvaluation, 'replies': repliesToIt})

django fixtures

say you have a directory “main” (an app), with models.py containing “class Evaluation”
you can create a fixture thus:

[
    {
        "pk": "1",
        "model": "main.Evaluation",
        "fields": {
            "user_who_made_it": "1",
            "user_they_evaluated": "2",
            "personal_comments": "first evaluation from 1 to 2",
            "is_positive": "0",
            "pub_date": "2006-06-16 12:00:00"
        }
    }

]

And put it in the main directory and voila. Works.

django duplicate field weirdness

So in django if you duplicate field names in a model declaration “weird” things happen, like
ValueError at /admin/

invalid literal for int() with base 10: ‘False’

Request Method: POST
Request URL: http://localhost:8000/admin/
Exception Type: ValueError
Exception Value: invalid literal for int() with base 10: ‘False’
Exception Location: c:\Python25\lib\site-packages\django\db\backends\util.py in typecast_date, line 46

Go figure 🙂

how to download files in Ruby

# the ruby way to download files 🙂 — useful if you haven’t got wget on the system…
require ‘open-uri’
writeOut = open(whereItGoes, “wb”)
writeOut.write(open(urlToGet).read)
writeOut.close
puts “downloaded” + urlToGet + “\n”

Of course, this reads in the whole file before writing it (I bet), so be careful.

setting up scgi

As a note on http://wiki.rubyonrails.org/rails/pages/SCGI+Rails+Runner+with+Apache+2+on+Linux

Note: you may not need to edit the .htaccess file at all.  Don’t be afraid if you do it and find an error messsage in apache/logs/error that the rewrite engine on directive is not recognized.  This means that mod_rewrite is not installed but it may not be necessary for SCGI anyway.  Apparently not necessary these days.

svn purge

svn purge is used to ‘delete’ all traces of a file (“it never existed”) from a repository.  One idea (since as of 2005 purge wasn’t implemented) is to keep some backups of the svn repostiory directory so you can ‘step back’ by reerting to a saved repository. not pretty but do-able.

ruby compilation overcome

So...
it appears that if you compile ruby and get a "warning clock skew detected" that it
 may be due to your machine having a clock set for a certain time, and an nfs server being set 
for (say) one hour difference or not. Synchronize them and you're good 🙂
Thanks for all those information.

background:

BTW I am using a intel box having 8 processors, each one 2-way hyperthreaded. And yes, the File system id      NFS.

Thanks.
--------
Hemayet Hossain

Dan Gibson wrote:

This happens when you're compiling on a virtual machine, and your host machine supports dynamic frequency modulation. There is a fix to the issue... you can find it on google.

Mike Marty wrote:
While Make is doing its timestamp-based dependency check, the timestamps
on some of your files appeared to be in the future...hence the clock skew
detected message

It is not a GEMS thing.  Sounds like an issue on your host system.

--Mike

Hi All,
I was compiling ruby (gems 1.3) in simics 3.0.14. At the end of
compilation I am getting the following warning. (ruby seems working
fine). Does anybody know why is that coming or is there any potential
problem for that warning?
---------------------------------
Template closure done
Made module
make[1]: warning:  Clock skew detected.  Your build may be incomplete.
make[1]: Leaving directory `/home/vax2/u2/hossain/G3S364/ruby'
Ruby: Linking simics module
cd ../simics; make ruby
make[1]: Entering directory `/home/vax2/u2/hossain/G3S364/workspace'
=== Building module "ruby" ===
make[2]: Entering directory
`/home/vax2/u2/hossain/G3S364/workspace/amd64-linux/obj/modules/ruby'
make[2]: Warning: File
`/home/vax2/u2/hossain/G3S364/workspace/modules/../../ruby/amd64-linux/generated/MOSI_SMP_bcast/obj/util.d'
has modification time 51 s in the future
Using the Simics 3.0 API for ruby module
CCLD    ruby.so
make[2]: warning:  Clock skew detected.  Your build may be incomplete.
make[2]: Leaving directory
`/home/vax2/u2/hossain/G3S364/workspace/amd64-linux/obj/modules/ruby'
make[1]: Leaving directory `/home/vax2/u2/hossain/G3S364/workspace'
Ruby: Moving simics module
make BUILD_FLAGS=-DSIMICS movemodule
make[1]: Entering directory `/home/vax2/u2/hossain/G3S364/ruby'
../scripts/prepare_simics_home.sh ../simics/home/MOSI_SMP_bcast amd64-linux
make[1]: Leaving directory `/home/vax2/u2/hossain/G3S364/ruby'
Ruby: my work is done here...

Thanks.

Roger's meanderings, notes to himself, bug reports, and other things