hi everybody
I'm making a custom form widget in the newforms admin and I want to
have access to the id of the current model instance (for already
existing data).
Can I get the id from the request object? Or any other solution?
> hi everybody
> I'm making a custom form widget in the newforms admin and I want to
> have access to the id of the current model instance (for already
> existing data).
> Can I get the id from the request object? Or any other solution?
def render(self, name, value, attrs=None): #value is the field you assign this widget to. #So, if it's an id or pk, you have it now. #You also have self.whatever to work with.
On Mon, 2008-11-24 at 13:07 +0200, Donn wrote: > Further info, > Looks like from within a widget there's no easy way to find the Form. I'd a > thunk there'd be a Parent attribute, but there ain't.
The way Django does this is fairly normal design practice. Normal OO implementation practice is the hierarchical dependencies only go one way, not having references both "down" and "up". The widget is at the bottom of the hierarchy and is intentionally a fairly simple object. All it has to know how to do is render itself into HTML, given an initial value. The "smarts" of a field (e.g. validation) lie in the Field class and tying fields together to make an holistic form is the Form class (and above or next to that is the ModelForm class that knows how to make a form from a model).
There's no need for a widget to know that something is a "model id", since widgets don't care about models or what an id is. All it wants to know is "what should I render?" However, one might want to pass the model id down as some kind of initial data to a field and thus to the widget for rendering.
When designing these sorts of things, it often helps to remember that the same data "value" can move through a number of semantic roles: it's a model id at the ModelForm level, say, but by the time a Widget subclass is dealing with it, it's just initial data. Keeps things in perspective.
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]
I want that only dogs (no other animals from other groups) are listed
in the foreignkey drop down box for the representative_animal (which
would be all animals without modification) when I'm editing the
AnimalGroup dogs.