Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Rendering a field value in a custom way [RSS Feed]

#1 Dec. 11, 2010 09:31:17

S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Rendering a field value in a custom way


Hi guys,
I want to be able to render a custom field.

It's supposed to extend a normal char field. I want to place certain
content in front of the field, depending upon the fields value.

As an example take a simple field that allows entering a number.
Instead of displaying the number, I would like to display star symbols
(e.g. entering 5 would return 5 star symbols when rendered in
templates).

Can I do that by extending the field? I do not want to setup any
template extensions, I want the field to act like that naturally.

I tried overwriting the field like this:

<code>
class inPlaceCharField(models.CharField):
def __init__(self, *args, **kwargs):
self.widget = ReCaptcha
self.required = True
super(models.CharField, self).__init__(*args, **kwargs)
def render(self):
return ";;;"
def __unicode__(self):
return "123"
def __str__(self):
return "1234"
def value_to_string(self, obj):
value = self._get_val_from_obj(obj)
return "12345"
</code>

But none of them worked - I always get the field's value rendered.
Which function do I need to overwrite?



Kind Regards and thanks for your answer in advance
Stephan

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 11, 2010 09:47:12

S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Rendering a field value in a custom way


Note: Why do I not to use filters?

1. It's nicer if the designer doesn't always have to put |(...) behind
the field name.
2. I do not know how to access the object passed in the filter. E.g.
if I use {{rating.stars|muBeautify}} the muBeautify filter would only
get a unicode "5", but how would it know which field that is gained
from? It needs to know that the values given comes from rating.stars,
since it should different results if e.g. rating.percent is used.

I could write different filters for each, but then there would be ~ 30
filters for the designer to memorize.

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Dec. 11, 2010 11:49:55

S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Rendering a field value in a custom way


Okay, I *think* I figured out a way to fix this.

I can add methods such as get_stars to the relevant model.

Then in that method I can do whatever I like with the string (e.g.
convert it to star symbols).


Would that be the recommended way to do this?


Kind Regards
Stephan

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Dec. 11, 2010 17:47:50

w.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Rendering a field value in a custom way


On Dec 11, 5:49 am, Scoox <stephan.f.mul...@gmail.com> wrote:
> Okay, I *think* I figured out a way to fix this.
>
> I can add methods such as get_stars to the relevant model.
>
> Then in that method I can do whatever I like with the string (e.g.
> convert it to star symbols).
>
> Would that be the recommended way to do this?
>
> Kind Regards
> Stephan

The "recommended" way would be to use a custom template tag (filter),
which you said you did not wish to do.

If I understand what you want (and I'm not sure that I do), then you
_should_ use a custom filter. It really is not much different--it's
simply a Python function that returns rendered content. You can pass
strings or objects as the args. It would be better in the long run,
because:

1. There is probably less overhead--you are rendering and doing some
logic inside the same function call, not separate ones.
2. You can have more specific control over the rendering.
3. You only have to write one method. You can put the logic inside
of it that determines what field/object sent it. If you go the model
method route, you will have to write the same basic logic for each
model. It's just more code to write/upkeep.

That said, you can use the model method plan, and do the same thing.
I did it before when starting out, but now try to keep model methods
strictly those which modify the model, and rendering/display methods
in filters, since that is where the rendering happens.

Wayne

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 Dec. 13, 2010 10:35:30

S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Rendering a field value in a custom way


Hi Wayne,
thanks for your answer.

I now created a function that applies the render functions dynamically
to the object. So get_* is automagically created.

You are right about the downsides of this approach. However my
designer really does not like filters, so I have to stick to the
approach.


Kind Regards
Scoox

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Rendering a field value in a custom way [RSS Feed]

Board footer

Moderator control

Enjoy the 19th of August
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support