[google-appengine] Re: Model field value choices - can the list be generated dynamically?
I think that the best way to address your problem would be using a ReferenceProperty to another object type where you list all the colors you wish to present as choices, does that work? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Google App Engine" group. To post to this group, send email to google-appengine@googlegroups.com To unsubscribe from this group, send email to google-appengine+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en -~--~~~~--~~--~--~---
[google-appengine] Re: Model field value choices - can the list be generated dynamically?
On Sat, Dec 27, 2008 at 10:07 PM, Martynas Brijunas wrote: > Why am I doing this? It is because I want to use it with the > db.djangoforms.ModelForm class. If I have a list of choices for a > field, the ModelForm automatically gives me a dropdown. If there is no > list of choices all I get is a plain text entry field. You probably want to use a ReferenceProperty to a Colour entity in your Product model. Then, customise your form something like this: class ProductEntryForm(djangoforms.ModelForm): class Meta: model = Product colour = djangoforms.ModelChoiceField(Colour, required=True, query=Colour.all().order('name')) You can put any GqlQuery you want in the query=... bit. Dave. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Google App Engine" group. To post to this group, send email to google-appengine@googlegroups.com To unsubscribe from this group, send email to google-appengine+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en -~--~~~~--~~--~--~---
[google-appengine] Re: Model field value choices - can the list be generated dynamically?
Hi Consultuning/David, > You probably want to use a ReferenceProperty to a Colour entity in > your Product model. Then, customise your form something like this: > > class ProductEntryForm(djangoforms.ModelForm): > class Meta: > model = Product > > colour = djangoforms.ModelChoiceField(Colour, required=True, > query=Colour.all().order('name')) > thank you very much for your suggestion to use the RefrenceProperty. I am halfway there now. The Product form now displays a dropdown. However, I am not really clear on how to specify what will be displayed in that dropdown. Here is what it is displaying currently when you open a dropdown which lets you select a SizeOption model. >-< <__main__.SizeOption object at 0x01C77FD0> <__main__.SizeOption object at 0x01C77E10> <__main__.SizeOption object at 0x01C77FD0> I would prefer something more human readable. Here is my model definition: class SizeOption(db.Model): code = db.StringProperty(required = True) html = db.StringProperty(required = True, multiline = True) class Product(db.Model): size_option = db.ReferenceProperty(SizeOption) class ProductForm(djangoforms.ModelForm): class Meta: model = Product size_option = djangoforms.ModelChoiceField(SizeOption, None) Thank you, Martin --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Google App Engine" group. To post to this group, send email to google-appengine@googlegroups.com To unsubscribe from this group, send email to google-appengine+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en -~--~~~~--~~--~--~---
[google-appengine] Re: Model field value choices - can the list be generated dynamically?
> thank you very much for your suggestion to use the RefrenceProperty. I > am halfway there now. The Product form now displays a dropdown. > However, I am not really clear on how to specify what will be > displayed in that dropdown. Here is what it is displaying currently > when you open a dropdown which lets you select a SizeOption model. > You can use a custom ModelChoiceField and override its label_from_instance() method: class SizeOptionChoiceField(ModelChoiceField): def label_from_instance(self, obj): return obj.code class ProductForm(djangoforms.ModelForm): class Meta: model = Product size_option = djangoforms.SizeOptionChoiceField(SizeOption, None) Alternatively, you can override the __unicode__ method of your model: class SizeOption(db.Model): code = db.StringProperty(required = True) html = db.StringProperty(required = True, multiline = True) def __unicode__(self): return self.code Cheers, Alex -- www.muspy.com --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Google App Engine" group. To post to this group, send email to google-appengine@googlegroups.com To unsubscribe from this group, send email to google-appengine+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en -~--~~~~--~~--~--~---
[google-appengine] Re: Model field value choices - can the list be generated dynamically?
On Sun, Dec 28, 2008 at 9:02 AM, Alexander Kojevnikov wrote: > You can use a custom ModelChoiceField and override its > label_from_instance() method: Nice one! The alternative is to define a __str__ method on your SizeOption class, since that is what is used to render the options in the default label_from_instance implementation. Dave. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Google App Engine" group. To post to this group, send email to google-appengine@googlegroups.com To unsubscribe from this group, send email to google-appengine+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en -~--~~~~--~~--~--~---
[google-appengine] Re: Model field value choices - can the list be generated dynamically?
Hi Alex/David, > Alternatively, you can override the __unicode__ method of your model: > >class SizeOption(db.Model): >code = db.StringProperty(required = True) >html = db.StringProperty(required = True, multiline = True) > >def __unicode__(self): >return self.code > > You can use a custom ModelChoiceField and > override its > Nice one! The alternative is to define a __str__ method on your > SizeOption class, since that is what is used to render the options in > the default label_from_instance implementation. your suggestions to override methods worked a treat. Overriding at the Model level suits me very well as it gives me flexibility. One final question (I hope): what is the difference in overriding the __unicode__ and __str__ methods? Best regards, Martin --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Google App Engine" group. To post to this group, send email to google-appengine@googlegroups.com To unsubscribe from this group, send email to google-appengine+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en -~--~~~~--~~--~--~---
[google-appengine] Re: Model field value choices - can the list be generated dynamically?
On Mon, Dec 29, 2008 at 12:24 AM, Martynas Brijunas wrote: >> Nice one! The alternative is to define a __str__ method on your >> SizeOption class, since that is what is used to render the options in >> the default label_from_instance implementation. Oops. I read Alex's email too quickly. He suggests exactly what I mentioned, except using __unicode__ instead of __str__. > your suggestions to override methods worked a treat. Overriding at the > Model level suits me very well as it gives me flexibility. One final > question (I hope): what is the difference in overriding the > __unicode__ and __str__ methods? Unless you're actually dealing with Unicode text, there won't be any practical difference. Check the Python docs for the full story. Dave. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Google App Engine" group. To post to this group, send email to google-appengine@googlegroups.com To unsubscribe from this group, send email to google-appengine+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en -~--~~~~--~~--~--~---
[google-appengine] Re: Model field value choices - can the list be generated dynamically?
> > your suggestions to override methods worked a treat. Overriding at the > > Model level suits me very well as it gives me flexibility. One final > > question (I hope): what is the difference in overriding the > > __unicode__ and __str__ methods? > > Unless you're actually dealing with Unicode text, there won't be any > practical difference. Check the Python docs for the full story. > Martynas, David is absolutely correct, you can use either of them. I suggest to always use the __unicode__ method unless you have a very good reason to use __str__. Cheers, Alex -- www.muspy.com --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Google App Engine" group. To post to this group, send email to google-appengine@googlegroups.com To unsubscribe from this group, send email to google-appengine+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en -~--~~~~--~~--~--~---
[google-appengine] Re: Model field value choices - can the list be generated dynamically?
I initially implemented using __str__ (without much thought). Then in some cases, started seeing unicode decode errors in template views if a user inputs international characters. Switching to __unicode__ resolved this category of issues. That was the practical difference, for me, between __str__ and __unicode__ in the models. On Dec 28, 1:20 pm, Alexander Kojevnikov wrote: > > > your suggestions to override methods worked a treat. Overriding at the > > > Model level suits me very well as it gives me flexibility. One final > > > question (I hope): what is the difference in overriding the > > > __unicode__ and __str__ methods? > > > Unless you're actually dealing with Unicode text, there won't be any > > practical difference. Check the Python docs for the full story. > > Martynas, > > David is absolutely correct, you can use either of them. I suggest to > always use the __unicode__ method unless you have a very good reason > to use __str__. > > Cheers, > Alex > --www.muspy.com --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Google App Engine" group. To post to this group, send email to google-appengine@googlegroups.com To unsubscribe from this group, send email to google-appengine+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en -~--~~~~--~~--~--~---
[google-appengine] Re: Model field value choices - can the list be generated dynamically?
Gents, I cannot thank you enough for all your help. The community here is great and makes a n00b like me feel welcome. Happy New Year! Best regards, Martin --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Google App Engine" group. To post to this group, send email to google-appengine@googlegroups.com To unsubscribe from this group, send email to google-appengine+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en -~--~~~~--~~--~--~---
[google-appengine] Re: Model field value choices - can the list be generated dynamically?
> You can use a custom ModelChoiceField and override itslabel_from_instance() > method: > > class SizeOptionChoiceField(ModelChoiceField): > deflabel_from_instance(self, obj): > return obj.code Sorry, I'm a bit late to the game but i just ran into this thread and wanted to ask that have you been able to use label_from_instance() in GAE? I used to use it Django but in GAE it just doesn't get called ever. That's why I have been using __unicode__ as well. Has anybody ran into this issue? Thanks --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Google App Engine" group. To post to this group, send email to google-appengine@googlegroups.com To unsubscribe from this group, send email to google-appengine+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en -~--~~~~--~~--~--~---
[google-appengine] Re: Model field value choices - can the list be generated dynamically?
Hi, On Jan 20, 5:49 pm, Janne Kuuskeri wrote: > > You can use a custom ModelChoiceField and override itslabel_from_instance() > > method: > > > class SizeOptionChoiceField(ModelChoiceField): > > deflabel_from_instance(self, obj): > > return obj.code > > Sorry, I'm a bit late to the game but i just ran into this thread and > wanted to ask that have you been able to use label_from_instance() in > GAE? I used to use it Django but in GAE it just doesn't get called > ever. That's why I have been using __unicode__ as well. Has anybody > ran into this issue? This *should* work in our app-engine-patch repository. http://www.bitbucket.org/wkornewald/appenginepatch-sample/ Project site: http://code.google.com/p/app-engine-patch/ Bye, Waldemar Kornewald --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Google App Engine" group. To post to this group, send email to google-appengine@googlegroups.com To unsubscribe from this group, send email to google-appengine+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en -~--~~~~--~~--~--~---