views:

226

answers:

3

I have a model with a field that is required but not entered by the user and i have a hard time saving the model without errors. My model definition looks like this:

class Goal(db.Model):
  author = db.UserProperty(required=True)
  description = db.StringProperty(multiline=True, required=True)
  active = db.BooleanProperty(default=True)
  date = db.DateTimeProperty(auto_now_add=True)

class GoalForm(djangoforms.ModelForm):
  class Meta:
    model = Goal
    exclude = ['author', 'active']

And i use django-forms in appengine to create and validate the form. When i try to save the result of this form however....

  def post(self):

    data = GoalForm(data=self.request.POST)

    if data.is_valid():

      goal = data.save(commit=False)
      goal.author = users.get_current_user()

      goal.put()
      self.redirect('/')

I get "ValueError: The Goal could not be created (Property author is required)"

Now i would think that by having commit=False, then adding the property for Goal, and then saving the object would allow me to do this but obviously it's not working. Any ideas?

A: 

Your GoalForm should inherit from django.forms.ModelForm and be defined such that it only requires some fields:

class GoalForm(django.forms.ModelForm):
    class Meta:
        model = Goal
        fields = ('description', 'etc')

Not sure if this is totally working in AppEngine though.

You should also save the form (still not sure about AppEngine):

data = GoalForm(data=self.request.POST)

if data.is_valid():
  data.author = users.get_current_user()
  data.put()
stefanw
yeah i do inherit djangoforms, i added my GoalForm to make that more clear, this should work in appengine from what i gather. except perhaps for this special case?
tijs
I extended my answer, it may work like that.
stefanw
data.put() wont's work since i will have to save() the form (which validates it) first and then use that data to put() the goal in the datastore.i did try simply adding the data.author = users.get_current_user() before i save it but that doesn't work. i basically have to get that author into the form data before i save it and it gets validated.
tijs
+1  A: 

Note that save() will raise a ValueError if the data in the form doesn't validate

You can find what you need about the save() method here:

http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-save-method

Edit: Instead of goal.put(), do a goal.save()

Edit2: This should solve your problem:

goal = Goal(author='Mr') #example
data = GoalForm(data=self.request.POST, instance=goal)
AlbertoPL
this is where i found how to add more properties before committing, but it does not say what to do in my particular case as far as i could tell. completing the model first and than passing that as an instance to the GoalForm fails with a similar error..
tijs
Try doing a goal.save(), not a goal.put()
AlbertoPL
aha ok thanks! tried it but i'm afraid that results in the same error: "ValueError: The Goal could not be created (Property author is required)"
tijs
See my new edit above
AlbertoPL
awesome! works like a charm. Thanks for that!
tijs
You're very welcome, I learned something new myself!
AlbertoPL
+1  A: 

I realize this is an old question, but for the sake of others searching for a similar answer, I'm posting the following:

Unless there's a reason I missed for not doing so, but I believe this is what you need:

class Goal(db.Model):
  author = db.UserProperty(auto_current_user_add=True)
  ...
  ...

For reference: Types and Property Classes:class UserProperty()

Chris Larson