Django Model Fields a repr_output property, to include a field in the string representation of an object

120 views
Skip to first unread message

Ben Friedland

unread,
Jun 23, 2016, 7:30:50 PM6/23/16
to Django developers (Contributions to Django itself)
Has a feature like this ever been considered? 

If a model has no __unicode__, __str__ or __repr__ representation, then maybe it could devise a string representation by collecting fields which have this value set to True. 

Example:

Without the feature:

class Person(models.Model):
     first_name = models.CharField(max_length=50)
     last_name = models.CharField(max_length=50)

>>> person = Person(first_name='Ben', last_name='Friedland')
>>> print person
<Person: Person object>    # fairly useless object representation


This feature would work something like: 

class Person(models.Model):
     first_name = models.CharField(max_length=50, repr_output=True)
     last_name = models.CharField(max_length=50, repr_output=True)

>>> person = Person(first_name='Ben', last_name='Friedland')
>>> print person
<Person: first_name='Ben', last_name='Friedland'>   # includes fields specified via repr_output=True

If this would be useful I'd be happy to formally create an issue and even implement the feature. 

Thanks!

Ben Friedland

Tim Graham

unread,
Jun 27, 2016, 9:24:57 AM6/27/16
to Django developers (Contributions to Django itself)
A new model field option doesn't seem necessary. I think a cleaner solution would be something like a decorator that takes a list of fields, e.g.

@repr_fields('first_name', 'last_name')
class Person(...):
     ...

This doesn't need to live in Django itself though.

Ben Friedland

unread,
Jul 1, 2016, 3:05:13 PM7/1/16
to Django developers (Contributions to Django itself)
Well in that case wouldn't it make sense to add it to the meta class? Just like how there's unique_together, etc., there could be repr_fields. In which case I think it would be great if it were part of Django. 

Maybe reconsider as a part of the meta class?

Ben Friedland

Tim Graham

unread,
Jul 1, 2016, 3:52:33 PM7/1/16
to Django developers (Contributions to Django itself)
Since no one else expressed interest in two weeks, I think it's safe to say it isn't a use case for 80% of users. That's the rough bar I think of as to whether or not something needs to be in core.

Erik Cederstrand

unread,
Jul 1, 2016, 4:43:38 PM7/1/16
to django-d...@googlegroups.com
Plus, it's 3 lines of code and faster to implement than to look up the documentation:


class ReprFieldsMixIn:
class Meta:
repr_fields = ('bar', 'baz')

def __repr__(self):
'<%s: %s>' % (self.__class__.__name__, ', '.join('%s=%s' % (f, repr(getattr(self, f))) for f in self._meta.repr_fields))


Erik
> --
> You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
> To post to this group, send email to django-d...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-developers.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/08be43ef-ef2e-4a62-bf17-a33935ab207b%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages