I'm just starting to use django and have run into a problem I have not
been able to solve.

I have a model Item which stores, among other things, user_id.
Then I have a ModelForm. I want user_id to be a hidden field. After
searching around the web, I found out how to create a hidden field.
The template is rendered as I like it.
user_id is a hidden field with a value of 1. The problem is when I
submit the form, I get IntegrityError. "Column 'user_id' cannot be
null".

but from the debug message I get back I can see that POST has all 3
things I need. So maybe when a new form is created there is problem.

views.py
@login_required
def newItem(request):
        if not request.POST:
                form = ItemForm()
                return render_to_response('manager/newItem.html',{'form':form})
        newForm = ItemForm(request.POST)
        if newForm.is_valid():
                newForm.save() #problem here?
                return HttpResponsePermanentRedirect('.')

models.py
class Item(models.Model):
        user = models.ForeignKey(User)
        name = models.CharField(max_length=32)
        description = models.CharField(max_length=128)
        creation_date = models.DateField(auto_now=True)

forms.py
class ItemForm(ModelForm):
    user_id_wid = forms.HiddenInput(attrs={'value': 1})
    user_id = forms.IntegerField(widget=user_id_wid,required=False)
    class Meta:
        model = Item
        fields = ['name', 'description']

and my template has:
<form action="newItem" method="POST">
        <fieldset>
                <legend>Create Item</legend>
                {% for field in form %}
                        {% if field.is_hidden %}
                                {{field}}
                        {% else %}
                                <div class="fieldWrapper">
                                        {{ field.errors }}
                                        {{ field.label_tag }}: {{ field }}
                                </div>
                        {% endif %}
                {% endfor %}
                <p><input type="submit" value="create"></p>
        </fieldset>
</form>

The hidden field is not being bound to the form? is that it? If so how
can I go about it?

NOTE: I can insert a new object if i manually create a form myself,
and just get the values from request.POST and do Item.objects.create()
with the POST values I get. I am just learning so I want to make sure
how to do this with ModelForms. That way i won't have to write as many
forms html either.

Thanks!

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to