Сокрытие поля в dhangoforms, которые генерируются из модели и сохранение его в базе данных - PullRequest
0 голосов
/ 20 марта 2011

1] У меня есть следующая модель:

class UserReportedData(db.Model):
  #country selected by the user, this will also populate the drop down list on the html page
  country = db.StringProperty(default='Afghanistan',choices=['Afghanistan'])
  #city selected by the user
  city = db.StringProperty()
  user_reported_boolean = db.BooleanProperty() # value that needs to be hidden from the display 
  date = db.DateTimeProperty(auto_now_add=True)

class UserReportedDataForm(djangoforms.ModelForm):

  class Meta:
    model = UserReportedData

2] HTML-код, который определит логическое значение "user_reported_boolean", выглядит следующим образом

 '<input type="submit" name="report_up" value= "Report Up">'
 '<input type="submit" name="report_down" value= "Report Down">'

3] Идея заключается весли нажата «report_up», логическое значение «user_reported_boolean» должно быть сохранено как true

4] Код, который получает вызов, когда пользователь отправляет, выглядит следующим образом

class UserReporting(webapp.RequestHandler):

  def post(self):
    #get the data that the user put in the django form UserReportForm
    data = UserReportedDataForm(data=self.request.POST)

    #need to find whether user hit the button with the name "report_up" or "report_down"
    # this is not working either
    if 'report_up' in self.request.POST:
        data.user_reported_boolean = True
    elif 'report_down' in self.request.POST:
        data.user_reported_boolean = False    

    if data.is_valid():
        # Save the data, and redirect to the view page
        entity = data.save(commit=False)
        entity.put()
        self.redirect('/')

Вопросы:

1] Как скрыть поле "user_reported_boolean" от отображения в html-форме

2] Как сохранить это поле "user_reported_boolean" в базе данных

1 Ответ

0 голосов
/ 20 марта 2011

1: исключить поле из вашей модели.

class MyModelForm(ModelForm):
    class Meta:
         model = Foo
         exclude = ('myfield',)

2: в несохраненном экземпляре модели через commit=False

entity = data.save(commit=False)
entity.user_reported_boolean = True # False, whatever.
entity.save()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...