Opened 17 years ago

Closed 14 years ago

#5931 closed Cleanup/optimization (fixed)

__repr__ for db Fields

Reported by: Thomas Güttler <hv@…> Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: hv@…, mathieu.agopian@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Hi,

for debugging it is usefull, if repr(mydbfield) displays the name
of the field. Small patch inclusive test attached.

Attachments (4)

db_models_fields_repr.diff (1.1 KB ) - added by Thomas Güttler <hv@…> 17 years ago.
5931.repr_for_model_fields.diff (1.3 KB ) - added by Johannes Dollinger 15 years ago.
added a test
5931-__repr__-for-db-fields (1.5 KB ) - added by Mathieu Agopian 14 years ago.
updated patch that applies to the current trunk
5931-__repr__-for-db-fields.diff (1.7 KB ) - added by Mathieu Agopian 14 years ago.
updated patch taking claudep's and julien's comments into account

Download all attachments as: .zip

Change History (16)

by Thomas Güttler <hv@…>, 17 years ago

Attachment: db_models_fields_repr.diff added

comment:1 by Matt McClanahan, 17 years ago

Component: UncategorizedDatabase wrapper
Triage Stage: UnreviewedDesign decision needed

comment:2 by Malcolm Tredinnick, 16 years ago

Patch needs improvement: set
Triage Stage: Design decision neededAccepted

Seems reasonable. However, the test doesn't show that it works as expected. It shows that when you use a field in a completely non-standard fashion (outside of a model), it reports None. That's maybe 0.001% of the normal use-cases. Testing the other 99%+ would be ideal. :-)

by Johannes Dollinger, 15 years ago

added a test

comment:3 by anonymous, 15 years ago

Patch needs improvement: unset

comment:4 by Gabriel Hurley, 14 years ago

Severity: Normal
Type: Cleanup/optimization

comment:5 by Julien Phalip, 14 years ago

Easy pickings: set
Patch needs improvement: set

This looks great but it'd be even better if the tests also covered the 0.001% of use cases (i.e. when the field does not have a name) :-)

comment:6 by Mathieu Agopian, 14 years ago

Cc: mathieu.agopian@… added
Patch needs improvement: unset

Here is an updated patch that applies on the current trunk.

I believe there's no way a django.db.models.fields.Field won't have a name anymore though.

by Mathieu Agopian, 14 years ago

Attachment: 5931-__repr__-for-db-fields added

updated patch that applies to the current trunk

comment:7 by Claude Paroz, 14 years ago

Patch needs improvement: set

I think that Julien's comment was referring to a test like:

from django.db.model.fields import CharField
my_field = CharField()
repr(my_field)

that should reveal the None value in place of the name.

in reply to:  7 comment:8 by Julien Phalip, 14 years ago

Replying to claudep:

I think that Julien's comment was referring to a test like:

from django.db.model.fields import CharField
my_field = CharField()
repr(my_field)

that should reveal the None value in place of the name.

That's correct. There's nothing preventing you from using a Field on its own, hence the "getattr(self, 'name', None))" in the patch ;)

comment:9 by Julien Phalip, 14 years ago

That said, I also think that <django.db.models.fields.CharField None> would be rather confusing. If the field doesn't have a name, then simply displaying <django.db.models.fields.CharField> would be preferable in my opinion.

by Mathieu Agopian, 14 years ago

updated patch taking claudep's and julien's comments into account

comment:10 by Mathieu Agopian, 14 years ago

Patch needs improvement: unset

I understand now ;) thanks for the feedbacks and comments!

I've attached an updated patch, with the additional test and the specific case for the Field instantiated without a name.

Please let me know if this needs further improvements!

comment:11 by Aymeric Augustin, 14 years ago

Triage Stage: AcceptedReady for checkin

comment:12 by Jannis Leidel, 14 years ago

Resolution: fixed
Status: newclosed

In [16145]:

Fixed #5931 -- Added repr to db fields. Thanks, Thomas Güttler, emulbreh and magopian.

Note: See TracTickets for help on using tickets.
Back to Top