10 messages in com.googlegroups.google-appengine[google-appengine] Re: Django Forms
FromSent OnAttachments
g-man26 Jul 2008 07:36 
g-man26 Jul 2008 19:23 
Q28 Jul 2008 07:16 
g-man28 Jul 2008 13:01 
Q29 Jul 2008 04:37 
g-man29 Jul 2008 13:00 
Q30 Jul 2008 05:14 
g-man30 Jul 2008 20:59 
Qaiser W. Muhammad31 Jul 2008 00:08 
g-man01 Aug 2008 12:23 
Subject:[google-appengine] Re: Django Forms
From:g-man (greg@gmail.com)
Date:07/29/2008 01:00:03 PM
List:com.googlegroups.google-appengine

Well, from the evidence, BadKeyError would seem to say you are sending over a 'bad key' ;)

Also, the 'bad string' says you are trying to send 'Molinex=' instead of something like 'Shoes - blue' or a true string.

Getting the form is only the first step. When I send back my for mada, I just save it as a string, then do a search on that string when I want to use it again.

So, my suggestion would be to look at the raw data and see what is going back to the view function, and why.

Django gives me a nice output of all the calls, so I can look into all the variables and headers. If you have 'DEBUG=True' in your settings.py you will see that.

Also, there are some browser tools you can get to reveal 'live http headers', and firebug will give you lots of info, too.

Try that and let us all know what you discover...

On Jul 29, 4:37 am, Q <qais@gmail.com> wrote:

Thanks for the help, it really put me on the right track. I am now able to view the correct data, however when trying to save the form, it is not saving the correct information, and gives me an error.

Exception Type: BadKeyError Exception Value: Invalid string key Molinex=.

any idea what maybe causing this?

On Jul 29, 1:01 am, g-man <greg@gmail.com> wrote:

Glad to give my beginner's knowledge. They say the best way to learn something is to teach it to someone else!

So, in your case, let's see:

--- product_form.py ---

from models import Product from models import ProductMakeModel

class ProductForm(djangoforms.ModelForm): # Build the choices, and create a drop-down list for ProductMakeModel selection/ removal. base_choice=('','none') choices=[(each_product.productMake, each_product.productMake) for each_product in db.Query(ProductMakeModel).order('name')] choices.insert(0,base_choice) product_make = forms.ChoiceField(choices=choices, required=False)

class Meta: model=Product

... this will give you the productMake only in the dropdown. It will be returned as the 'product_make' parameter, which you can manipulate in the view. You can decide if you need the 'required' validation yourself.

I see you have both 'make' and 'model' in one instance; which one governs? If you want to display both in one selection, then you will have to create a new string, combining them both, and then even filter for your 'active' flag (if the product is no longer available).

I guess it would look like 'shoes - red', 'shoes - blue', right?

This was fun for me, to think about a new idea. It also shows how important the correct names, file organization, model concepts, etc are. I like the product_make approach, with the ProductMake style reserved for class names only (I think this is Python style).

Try it out and check back with your results! The best part is that the dev_appserver lets you try things quickly with no reloading because of scripts, etc.

On Jul 28, 7:17 am, Q <qais@gmail.com> wrote:

Thanks for the code, this gives me some idea on how to do this, however since i am so new, i am still a bit confused.

The model i have is as follows:

class ProductMakeModel(db.Model): productMake = db.StringProperty(multiline = False) productModel = db.StringProperty(multiline = False) productMakeModelActive = db.BooleanProperty()

class Product(db.Model): owner = db.ReferenceProperty(Owner) productModel = db.ReferenceProperty(ProductMakeModel) productColor = db.StringProperty(multiline = False)

The form that i want to build is for Product, so in order for me to choose the correct MakeModel for this, how should i proceed.

On Jul 27, 7:24 am, g-man <greg@gmail.com> wrote:

So, here's my code to do this:

class ItemForm(djangoforms.ModelForm): # Build the choices, and create adrop-downlist for Tag selection/ removal. base_choice=('','none') choices=[(each_tag.name, each_tag.name) for each_tag in db.Query(Tag).order('name')] choices.insert(0,base_choice) add_or_remove_tag =forms.ChoiceField(choices=choices, required=False)

class Meta: model=Item

... what I did is create acustomfield within the form, before the Meta, which is populated by the names of my tags, harvested by a query of the reference Model. Notice I added a default choice in there for the first position. The 'add_or_remove_tag' value comes back from the form template as a parameter, which I just append to my list of tags for the particular item.

You can't actually see this in operation on my app, because I'm just doing 'admin-added' blog post Items right now, but it works fine; that's where all the tags you will see there come from.

On Jul 26, 4:42 am, Q <qais@gmail.com> wrote:

I am very new to Django and AppEngine and my question my sound very silly to some.

I am trying to build a form which uses ReferenceProperty.

my current model contains

class Owner(db.Model): ownerName = db.StringProperty(multiline = False) ownerAddress = db.PostalAddressProperty() ownerPhoneNumber1 = db.PhoneNumberProperty()

class Product(db.Model): ower = db.ReferenceProperty(Owner) productColor = db.StringProperty(multiline = False)

When i use the following form

class ProductForm(djangoforms.ModelForm): class Meta: model = Product

With this setup when i access my form, i get adropdownwith bunch of garbage where as I want the Owner name which was saved in the first model. Can someone help me with this?