Re: AW: __str__, __unicode__ representation of model fields

2008-11-04 Thread Donn

On Monday, 03 November 2008 10:50:34 Bülent Aldemir wrote:
> I want to have a nice representation for the other MyModel() fields. Must I
> define for each field a method to accomplish my task?
Seems the best way. There's only one 'repr' for any object and if you want 
specific ones then either make a single def that takes an arg (an instance of 
the field you want to format or a string to match in the def) and then have 
it spit out a formatted value in response, or have a def per field.

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



AW: __str__, __unicode__ representation of model fields

2008-11-03 Thread Bülent Aldemir

Thanks for your reply.

Maybe my question was a bit misleading: where do I put default
representations for each model field on model level (not templates). 

I want to have a nice representation for the other MyModel() fields. Must I
define for each field a method to accomplish my task?


class MyModel(models.Model):
     name = models.CharField(max_length=150)
     myinteger = models.IntegerField()

     def __unicode__(self):
         return u"Here is my: %s" % (self.name)
 
 def my_nice_integer(self):
 return u"%s" % str(self.myinteger).zfill(10)

Is there a alternative? Does someone know how django formats for example
dates on the admin pages? 

Thanks
Bülent
 


-Ursprüngliche Nachricht-
Von: django-users@googlegroups.com [mailto:[EMAIL PROTECTED] Im
Auftrag von JoeJ
Gesendet: Montag, 3. November 2008 00:02
An: Django users
Betreff: Re: __str__, __unicode__ representation of model fields



To get an integer to print with leading 0s print '%0d' % my_model.myinteger

To get an integer to print with 10 digits, filled with leading 0s print
'%0.10d' % my_model.myinteger

If you wanted this to be the default value shown for the model def
__unicode__(self): return u'%0.10d' % self.myinteger

-- joe

On Nov 2, 1:30 pm, Bülent Aldemir <[EMAIL PROTECTED]> wrote:
> Hi,
>
> given
>
> class MyModel(models.Model):
>     name = models.CharField(max_length=150)
>     myinteger = models.IntegerField()
> ^
>     def __unicode__(self):
>         return u"Here is my: %s" % (self.name)
>
> one has to give a __unicode__ method to get a nice string 
> representation of
> MyModel()
>
> I need a nice string representation of "myinteger" too, e.g.
> str(myinteger).zfill(10)
>
> I want to be able to do just this:
>
> >> my_model = MyModel.objects.get(pk=1) print my_model Here is my: 
> >> Name print my_model.myinteger '01'
>
> I do not know where to put the hook. Can anyone give me a hint, where 
> I can find documentation and/or what to do to accomplish my task.
>
> Thanks!
> Bülent



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