[Ncr-Python.in] why does to_python gets called twice in this code ?
hi I am struck trying to write a simple custom django field that inherits ForeignKey ..i posted the same at stackoverflow.. http://stackoverflow.com/questions/14214530/why-is-to-python-called-twice-in... any help or pointers are welcome. regards Vijay Shanker
On 8 January 2013 18:02, Vijay Shanker <deontics@gmail.com> wrote:
hi I am struck trying to write a simple custom django field that inherits ForeignKey ..i posted the same at stackoverflow..
http://stackoverflow.com/questions/14214530/why-is-to-python-called-twice-in... any help or pointers are welcome.
It is hard to definitively tell why to_python() is called twice, without digging through the Django admin. code. My guess is that: * As you have set SubfieldBase metaclass, it is called once when the field is created. * It is called again on form validation: "The to_python() method on a Field is the first step in every validation." https://docs.djangoproject.com/en/dev/ref/forms/validation/ However, the problem probably lies in your code, which seems a bit confused for the ProfiledUserField class: * to_python() should be prepared to accept values of None, if null=True is allowed. Please see the note at the bottom of the section https://docs.djangoproject.com/en/dev/howto/custom-model-fields/#converting-... * Your to_python() function seems to serve no purpose. For a ForeignKey field that you derive from, the returned value should be an integer, and not an instance of User as you seem to be assuming.. Thus, the to_python() function, as in the base class (deriving from django.db.models.Field), could simply be: def to_python(self, value): return value I.e., there is no need to override this function that I can see. * Your __init__() function serves no purpose either: max_length is not a valid keyword argument for django.db.models.fields.related.ForeignKey I stopped reading at this point as it is increasingly seeming like a XY problem: http://people.apache.org/~hossman/#xyproblem People might better be able to help you if you would explain what you are trying to achieve. Regards, Gora
hi i rephrased the question.thanks for the help extended so far. http://stackoverflow.com/questions/14214530/issues-in-example-custom-field-i... regards vijay shanker On Tue, Jan 8, 2013 at 10:36 PM, Gora Mohanty <gora@mimirtech.com> wrote:
On 8 January 2013 18:02, Vijay Shanker <deontics@gmail.com> wrote:
hi I am struck trying to write a simple custom django field that inherits ForeignKey ..i posted the same at stackoverflow..
http://stackoverflow.com/questions/14214530/why-is-to-python-called-twice-in...
any help or pointers are welcome.
It is hard to definitively tell why to_python() is called twice, without digging through the Django admin. code. My guess is that: * As you have set SubfieldBase metaclass, it is called once when the field is created. * It is called again on form validation: "The to_python() method on a Field is the first step in every validation." https://docs.djangoproject.com/en/dev/ref/forms/validation/
However, the problem probably lies in your code, which seems a bit confused for the ProfiledUserField class: * to_python() should be prepared to accept values of None, if null=True is allowed. Please see the note at the bottom of the section https://docs.djangoproject.com/en/dev/howto/custom-model-fields/#converting-... * Your to_python() function seems to serve no purpose. For a ForeignKey field that you derive from, the returned value should be an integer, and not an instance of User as you seem to be assuming.. Thus, the to_python() function, as in the base class (deriving from django.db.models.Field), could simply be: def to_python(self, value): return value I.e., there is no need to override this function that I can see. * Your __init__() function serves no purpose either: max_length is not a valid keyword argument for django.db.models.fields.related.ForeignKey
I stopped reading at this point as it is increasingly seeming like a XY problem: http://people.apache.org/~hossman/#xyproblem People might better be able to help you if you would explain what you are trying to achieve.
Regards, Gora _______________________________________________ http://mail.python.org/mailman/listinfo/ncr-python.in Mailing list guidelines : http://python.org.in/wiki/NcrPython/MailingListGuidelines
Your question still has inconsistencies and lack of clarity. Have replied on SO itself. On Wed, Jan 9, 2013 at 4:07 PM, Vijay Shanker <deontics@gmail.com> wrote:
hi i rephrased the question.thanks for the help extended so far.
http://stackoverflow.com/questions/14214530/issues-in-example-custom-field-i... regards vijay shanker
On Tue, Jan 8, 2013 at 10:36 PM, Gora Mohanty <gora@mimirtech.com> wrote:
On 8 January 2013 18:02, Vijay Shanker <deontics@gmail.com> wrote:
hi I am struck trying to write a simple custom django field that inherits ForeignKey ..i posted the same at stackoverflow..
http://stackoverflow.com/questions/14214530/why-is-to-python-called-twice-in...
any help or pointers are welcome.
It is hard to definitively tell why to_python() is called twice, without digging through the Django admin. code. My guess is that: * As you have set SubfieldBase metaclass, it is called once when the field is created. * It is called again on form validation: "The to_python() method on a Field is the first step in every validation." https://docs.djangoproject.com/en/dev/ref/forms/validation/
However, the problem probably lies in your code, which seems a bit confused for the ProfiledUserField class: * to_python() should be prepared to accept values of None, if null=True is allowed. Please see the note at the bottom of the section https://docs.djangoproject.com/en/dev/howto/custom-model-fields/#converting-... * Your to_python() function seems to serve no purpose. For a ForeignKey field that you derive from, the returned value should be an integer, and not an instance of User as you seem to be assuming.. Thus, the to_python() function, as in the base class (deriving from django.db.models.Field), could simply be: def to_python(self, value): return value I.e., there is no need to override this function that I can see. * Your __init__() function serves no purpose either: max_length is not a valid keyword argument for django.db.models.fields.related.ForeignKey
I stopped reading at this point as it is increasingly seeming like a XY problem: http://people.apache.org/~hossman/#xyproblem People might better be able to help you if you would explain what you are trying to achieve.
Regards, Gora _______________________________________________ http://mail.python.org/mailman/listinfo/ncr-python.in Mailing list guidelines : http://python.org.in/wiki/NcrPython/MailingListGuidelines
_______________________________________________ http://mail.python.org/mailman/listinfo/ncr-python.in Mailing list guidelines : http://python.org.in/wiki/NcrPython/MailingListGuidelines
On 9 January 2013 16:07, Vijay Shanker <deontics@gmail.com> wrote:
hi i rephrased the question.thanks for the help extended so far. http://stackoverflow.com/questions/14214530/issues-in-example-custom-field-i... [...]
Sorry, but if you are asking on this list, please follow up here. At least I have no intention of following up on some closed-source site, no matter how nice it might be. If you want to ask on Stackoverflow, please stick to that site. Even from looking at your update on Stackoverflow, I still do not seem to see where you have described what you are trying to achieve, and are instead asking the very same question over again without looking at replies on this list. Regards, Gora
On Wed, Jan 9, 2013 at 4:07 PM, Vijay Shanker <deontics@gmail.com> wrote:
i rephrased the question.thanks for the help extended so far.
http://stackoverflow.com/questions/14214530/issues-in-example-custom-field-i...
Mention what you are trying to achieve when you ask a question. Otherwise how somebody can help you? Seeing your implementation I don't get any idea what you are trying to do. It doesn't look like something that requires a custom ModelField. Anyway the i guess the reason that it is getting called twice is, you have __metaclass__ django.db.models.SubFieldBase which will call to_python() every time you try to set an attribute in a class which is created by metaclass SubFieldBase. In the course of execution some where as part of validation it is trying to access the 'editor_id' which will give an AttributeError, followed by a setattr on that ProfiledUserField in turn to_python gets called with a None value. I didn't investigate much so my guess might be wrong. Try using a pdb and follow the path. PS: If you are mentioning what you want to achieve, there may be better solutions than writing custom Model Field. -- blog : ragsagar.wordpress.com mail id : python -c "print '@'.join(['ragsagar','.'.join([x for x in ['gmail','com']])])"
participants (4)
-
Gora Mohanty -
Nandeep Mali -
ragsagar -
Vijay Shanker