thanks for the replies donn and malcolm, here in more detail what I
want to archive
perhaps I'm on the totally wrong track...
How do I pass the PASSANYTHINGHERE=whatever parameters to the widget
(see sample code)?
(the super part is clear, thanks)
How to pass the "value" id on ModelForm level to the widget level? bad
idea?

here is my use-case:
Here is my imaginary code with animals, and how I thought it could
work, but doesn't.
I want to set a representative_animal (Animal) for each AnimalGroup.
How can I archive this? I tried to access the id for the current model
instance,
but couldn't access it from the widget level.

When I add multiple kinds of cats and dogs (sheep dog, terrier dog,
persian cat, ...)
to animals, I want that in the AnimalGroup for dogs, only kinds of
dogs are listed
as as potential representative_animal and no other animals.

I can use the "value" parameter which is passed to the widget (fk_id
of an animal)
for finding related animals of the same group, but this works only if
there is
already set an animal as representative_animal.
For empty values I need the AnimalGroup instance model id for finding
the related animals.

[CODE]
#models.py
from django.db import models

class AnimalGroup(models.Model):
    name = models.CharField(max_length=128)
    representative_animal = models.ForeignKey('Animal', blank=True,
null=True)
    def __unicode__(self):
        return self.name

class Animal(models.Model):
    name = models.CharField(max_length=128)
    lives_in_herds = models.BooleanField()
    animal_group = models.ForeignKey(AnimalGroup)
    def __unicode__(self):
        return self.name


#admin.py
from django.contrib import admin
from django import forms
from tapp.models import *

class SelectFiltered(forms.Select):
    def render(self, name, value, attrs=None, choices=()):
        model_id = ???? # where to get it?
        choices = None # reset default values
        if value is None:
            value = ''
            choices = ()
        else:
            animal_group = AnimalGroup.objects.filter(id=model_id)[0]
            choices = [(item.id, item.name) for item in
animal_group.animal_set.all()]
        final_attrs = self.build_attrs(attrs, name=name)
        output = [u'<select%s>' % flatatt(final_attrs)]
        if value:
            self.choices = choices
        options = self.render_options(list(choices), [value])
        if options:
            output.append(options)
        output.append('</select>')
        return mark_safe(u'\n'.join(output))
[/CODE]

regards
yves
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to