Re: Show HTML in Labels for Model Choice Fields

2013-05-22 Thread Guillermo Siliceo
Hey Adi your answer saved me lots of work, but i want to elaborate on it, i 
had this use case but i didnt have to subclass the field all i did was 
defined a new label_from_instance

def Option_with_specie(self):
return mark_safe(" %s 
"%(self.species.name, self.pk, self.name)) 

And then on the forms init:

class PetForm(forms.ModelForm):

def __init__(self, *args, **kwargs):
super(PetForm, self).__init__(*args, **kwargs)
if 'instance' in kwargs:
self.fields['breed'].label_from_instance = Option_with_specie

So now i customized the select field outputted by the foreing key, thanks 
to you Adi for finding just the right method to override.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




Re: Show HTML in Labels for Model Choice Fields

2011-01-02 Thread Adi
I was able to find the solution to it myself.

The key was to
1. Create a custom field that extended ModelChoiceField.
2. Overwrite the label_from_instance method in the custom field.
3. Return a HTML string from the label_from_instance method, and
marking that as a safe_string.


from django.forms import ModelChoiceField
from django.utils.safestring import mark_safe

class ThemeChoiceField(ModelChoiceField):
def label_from_instance(self,obj):
return mark_safe("%s - Click here to see an example. \
 " \
% (obj.name, obj.pk, obj.pk, obj.image.url))

-Adi


On Dec 30 2010, 6:36 pm, Adi  wrote:
> Hi I have a situation like this
>
> class AppearanceThemesForm(forms.Form):
>     page_theme =
> forms.ModelChoiceField(widget=forms.RadioSelect(attrs={'title':'Select
> the appropriate theme for your pages.'}),
>
> queryset=Theme.objects.filter(section="1"),
>
> initial=Theme.objects.get(section="1",representation="").pk,
>                                         label="Page Theme")
>
> And my Theme model is like this:
>
> class Theme(models.Model):
>     name = models.CharField(max_length=32)
>     image = models.ImageField(upload_to="images/themes", null=True)
>
>     def __unicode__(self):
>         return "%s" % (self.name)
>
> When I render AppearanceThemesForm in my template, i would like to be
> able to show a link next to the label. If a user clicks the link, the
> webpage displays the image associated with Theme.
>
> I tried putting in a custom field that extends the ModelChoiceField,
> and in the label_from_instance method, tried to output the HTML, but
> that gets escaped.
>
> What is the proper way to achieve what i am trying to do?
>
> Alternatively, how do i get access the underlying model object behind
> the modelchoice field in my templates?

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



Show HTML in Labels for Model Choice Fields

2010-12-30 Thread Adi
Hi I have a situation like this

class AppearanceThemesForm(forms.Form):
page_theme =
forms.ModelChoiceField(widget=forms.RadioSelect(attrs={'title':'Select
the appropriate theme for your pages.'}),
 
queryset=Theme.objects.filter(section="1"),
 
initial=Theme.objects.get(section="1",representation="").pk,
label="Page Theme")

And my Theme model is like this:

class Theme(models.Model):
name = models.CharField(max_length=32)
image = models.ImageField(upload_to="images/themes", null=True)

def __unicode__(self):
return "%s" % (self.name)


When I render AppearanceThemesForm in my template, i would like to be
able to show a link next to the label. If a user clicks the link, the
webpage displays the image associated with Theme.

I tried putting in a custom field that extends the ModelChoiceField,
and in the label_from_instance method, tried to output the HTML, but
that gets escaped.

What is the proper way to achieve what i am trying to do?

Alternatively, how do i get access the underlying model object behind
the modelchoice field in my templates?

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