Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
access current model id, for custom widget
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
yves_s  
View profile  
 More options Nov 20 2008, 6:24 pm
From: yves_s <yves.serr...@gmail.com>
Date: Thu, 20 Nov 2008 15:24:35 -0800 (PST)
Local: Thurs, Nov 20 2008 6:24 pm
Subject: access current model id, for custom widget
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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
yves_s  
View profile  
 More options Nov 24 2008, 5:21 am
From: yves_s <yves.serr...@gmail.com>
Date: Mon, 24 Nov 2008 02:21:55 -0800 (PST)
Local: Mon, Nov 24 2008 5:21 am
Subject: Re: access current model id, for custom widget
does anybody have some hints?

On 21 Nov., 00:24, yves_s <yves.serr...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Donn  
View profile  
 More options Nov 24 2008, 5:53 am
From: Donn <donn.in...@gmail.com>
Date: Mon, 24 Nov 2008 12:53:08 +0200
Local: Mon, Nov 24 2008 5:53 am
Subject: Re: access current model id, for custom widget
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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Donn  
View profile  
 More options Nov 24 2008, 6:07 am
From: Donn <donn.in...@gmail.com>
Date: Mon, 24 Nov 2008 13:07:21 +0200
Local: Mon, Nov 24 2008 6:07 am
Subject: Re: access current model id, for custom widget
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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Malcolm Tredinnick  
View profile  
 More options Nov 24 2008, 6:56 am
From: Malcolm Tredinnick <malc...@pointy-stick.com>
Date: Mon, 24 Nov 2008 22:56:05 +1100
Local: Mon, Nov 24 2008 6:56 am
Subject: Re: access current model id, for custom widget

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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
yves_s  
View profile  
 More options Nov 24 2008, 10:06 am
From: yves_s <yves.serr...@gmail.com>
Date: Mon, 24 Nov 2008 07:06:13 -0800 (PST)
Local: Mon, Nov 24 2008 10:06 am
Subject: Re: access current model id, for custom widget
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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Donn  
View profile  
 More options Nov 24 2008, 12:51 pm
From: Donn <donn.in...@gmail.com>
Date: Mon, 24 Nov 2008 19:51:53 +0200
Local: Mon, Nov 24 2008 12:51 pm
Subject: Re: access current model id, for custom widget
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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
yves_s  
View profile  
 More options Nov 25 2008, 2:01 pm
From: yves_s <yves.serr...@gmail.com>
Date: Tue, 25 Nov 2008 11:01:30 -0800 (PST)
Local: Tues, Nov 25 2008 2:01 pm
Subject: Re: access current model id, for custom widget
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-limitchoicesto...
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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »