Reputation: 1275
I am using UpdateView of Django (1.5.8) to update an object in my database. I edit the question object and hit submit; it doesn't give me any errors but it doesn't update the question either.
I have my QuestionUpdate class in views.py which extends to UpdateView:
class QuestionUpdate(UpdateView):
model = Question
form_class = QuestionForm
success_url = '.'
def get_context_data(self, **kwargs):
context = super(QuestionUpdate, self).get_context_data(**kwargs)
return context
I call QuestionUpdate in urls.py with this:
url(r'^question/(?P<pk>\d+)/$',QuestionUpdate.as_view(), name='question_update', ),
In the template, I use Bootstrap form-group. I display a text area with a default value which is the original question. Here is my template (I copy only the related part-the form):
<form action = "" method ="post"> {% csrf_token %}
<div class='form-group'>
<label>Question</label>
<textarea class='form-control' rows="3">{{question}}</textarea>
</div>
<input type = 'submit' value = 'Submit' />
</form>
I found this, but it doesn't help me so much. What am i missing?
There are two things I suspect:
I pass the pk to the url when I display the questions with updateview. I saw couple UpdateView examples using reverse() on the url. I don't know if I need it or why I need it. That might be the problem?
In my form, I display only the question_text, but there are other fields in the Question model. Am I having trouble because of the 'hidden fields'?
EDIT: Here are the Question model and QuestionForm model form:
models.py:
class Question(models.Model):
question_text = models.TextField()
question_type = models.CharField(choices = Option_type, max_length = 64)
calculate = models.CharField(choices=Calculation, blank = True, max_length = 64)
ordering = models.IntegerField()
scale_id = models.IntegerField()
scale = models.ForeignKey(Scale)
max_score_of_question = models.IntegerField()
showQues = models.IntegerField(default = 1)
def __unicode__(self):
return self.question_text
forms.py
class QuestionForm(ModelForm):
class Meta:
model = Question
# fields = ('question_text',)
Upvotes: 1
Views: 1212
Reputation: 9359
The problem is that you've columns scale_id
and scale
. See - if there's a ForeignKey
, it generates also a property named as the column + a suffix _id
, in this case scale_id
collides with the column scale_id
which is already there.
Just rename or remove one of them.
Upvotes: 1