Re: access current model id, for custom widget

2008-11-25 Thread yves_s

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.

I finally hacked something together with the threadlocals middleware,
which is I found here:
http://stackoverflow.com/questions/160009/django-model-limitchoicestouser-user
see cookbook link on the bottom

Perhaps the patch at 2445 ( http://code.djangoproject.com/ticket/2445
) would also work but I don't wanted to
patch django.

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
-~--~~~~--~~--~--~---



Re: access current model id, for custom widget

2008-11-24 Thread Donn

Sorry for being dense, but I don't really follow what you need. Look in the 
docs for backward relationships between foreign keys. That may help you.

Something like:
group = Animal.objects.get(animal_group__id=XX)

But I don't know where you decide what XX is.

\d

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: access current model id, for custom widget

2008-11-24 Thread yves_s

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'' % flatatt(final_attrs)]
if value:
self.choices = choices
options = self.render_options(list(choices), [value])
if options:
output.append(options)
output.append('')
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
-~--~~~~--~~--~--~---



Re: access current model id, for custom widget

2008-11-24 Thread Malcolm Tredinnick


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.

Regards,
Malcolm



--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: access current model id, for custom widget

2008-11-24 Thread Donn

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.

So:

If you are doing something like this:

class SomeWidget( forms.TextInput ):
def __init__(self,attrs=None, PASSANYTHINGHERE=whatever ):
self.whatever = whatever # Helpful stuff
super(fklookupwidget,self).__init__(attrs)

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.

hth,
\d

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: access current model id, for custom widget

2008-11-24 Thread Donn

On Monday, 24 November 2008 12:21:55 yves_s wrote:
> does anybody have some hints?
IIRC - You can do:
form.instance (inside your widget, I think...)

This assumes you made a form with something like:
f = MyForm(instance = someInstance)

On the command-line, try using:
python manage.py shell
Then import your form module and use the dir() command to look around at 
what's available.)

hth,
\d

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: access current model id, for custom widget

2008-11-24 Thread yves_s

does anybody have some hints?

On 21 Nov., 00:24, yves_s <[EMAIL PROTECTED]> wrote:
> 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?
>
> I'm really going mad with this problem..
>
> 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
-~--~~~~--~~--~--~---



access current model id, for custom widget

2008-11-20 Thread yves_s

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?

I'm really going mad with this problem..

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
-~--~~~~--~~--~--~---