Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Wedge of Lime
Sep 4, 2003

I lack indie hair superpowers.

Wulfeh posted:

checkboxes and newforms.

Interestingly I recently wanted to select the available check boxes based on data passed to my view. This isn't that well documented so I thought i'd share my example.

forms:
code:
class SampleForm(forms.Form):
    test_field = forms.ModelMultipleChoiceField(queryset=Model.objects.none())
Then in my view I only want to display a subset of the Model objects, I discovered this can be done as follows:

views:
code:
form = SampleForm()
form.fields['test_field'].queryset = Model.objects. # create any query set you wish  here
Fairly straight forward once you've seen it done.

Adbot
ADBOT LOVES YOU

Wedge of Lime
Sep 4, 2003

I lack indie hair superpowers.

nbv4 posted:

Does anyone have any experience using django-tagging with django 1.0? Is it stable? or alternatively a tagging system that works well with 1.0?

The following bug caused me some pain:

http://code.google.com/p/django-tagging/issues/detail?id=160

Also, there are a couple of issues with the parsing of tags if you aren't forcing lowercase tags.

All in all its a great library and I'm happily using it with Django 1.0.

Wedge of Lime
Sep 4, 2003

I lack indie hair superpowers.

hitze posted:

I was wondering if anyone knew how to do a simple for loop that iterates the number it gets from say "bingodingo.number". I've searched djangoproject.com and found a ticket about it. I can't seem to find any documentation on how to make the for loops in django work like that :suicide:

This is more to do with python than with Django its self, in python you are encouraged to loop through all elements of a list (or in Django a query result set) instead of using an iterator (as I'm sure you've found). So first I'd look at avoiding this in some way.

However, if you want to perform a loop x times you could use something like the built-in xrange function in your view. Then pass the resulting xrange object to your template.

code:
number = 5
for i in xrange(number):
    print i
    # do something else...
Though, I find that in most situations I can avoid using xrange and end up with simpler to read / maintain code.

Wedge of Lime fucked around with this message at 23:42 on Feb 18, 2009

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply