Accessing Model field from ModelForm?

2008-10-31 Thread killsto

I have a basic form that shows what a person entered when it is
submitted.  However, it shows up as a form.CharField (textbox) instead
of model.CharField. Is there a way to access the model's saved data
from the Forms? Also does form_for_model (http://www.djangobook.com/en/
1.0/chapter07/) no longer exist? I can't get it to load.

models.py:
from django.db import models
from django.forms import ModelForm

class Question(models.Model):
question = models.CharField('Question', max_length = '200',
primary_key = True)
pub_date = models.DateField(auto_now_add = True)
def __unicode__(self):
return self.question

class Choice(models.Model):
Question = models.ForeignKey(Question)
choice = models.CharField(max_length = '200')

class Questionform(ModelForm):
class Meta:
model = Question
class Choiceform(ModelForm):
class Meta:
model = Choice

view:
from qotd.models import *
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect

def contact(request, question):
if request.method == 'POST': # If the form has been submitted...
form = Questionform(request.POST) # A form bound to the POST
data
if form.is_valid():
form.save()
#return HttpResponseRedirect('results.html', {'form': form}

else:
form = Questionform()
return render_to_response('contact.html', {
'form': form
})
template:


{% if not form.is_valid %}

{{ form.as_p }}

{% endif %}
{% if form.is_valid %}
{{ form.question }}
{% endif %}




--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Accessing Model field from ModelForm?

2008-11-01 Thread killsto

Thanks, that's a big help.

On Nov 1, 5:14 am, Daniel Roseman <[EMAIL PROTECTED]>
wrote:
> On Nov 1, 5:33 am, killsto <[EMAIL PROTECTED]> wrote:
>
>
>
> > I have a basic form that shows what a person entered when it is
> > submitted.  However, it shows up as a form.CharField (textbox) instead
> > of model.CharField. Is there a way to access the model's saved data
> > from the Forms? Also does form_for_model (http://www.djangobook.com/en/
> > 1.0/chapter07/) no longer exist? I can't get it to load.
>
> 
>
> > from qotd.models import *
> > from django.shortcuts import render_to_response
> > from django.http import HttpResponseRedirect
>
> > def contact(request, question):
> >     if request.method == 'POST': # If the form has been submitted...
> >         form = Questionform(request.POST) # A form bound to the POST
> > data
> >         if form.is_valid():
> >                 form.save()
> >                 #return HttpResponseRedirect('results.html', {'form': form}
>
> >     else:
> >         form = Questionform()
> >     return render_to_response('contact.html', {
> >         'form': form
> >     })
>
> The only thing you need to do that you're not doing is to actually
> pass the instance of Question (or Choice) to the form when you
> instantiate it.
>
> Assuming that 'question' is the PK of the Question object:
>
> def contact(request, question):
>     question_obj = Question.objects.get(pk=question)
>     if request.method == 'POST':
>         form = Questionform(request.POST, instance=question_obj)
>         if form.is_valid():
>                 form.save()
>                 return HttpResponseRedirect('/submission/')
>     else:
>         form = Questionform(instance=question_obj)
>     return render_to_response('contact.html', {
>         'form': form
>     })
>
> Note that HttpResponseRedirect takes a *URL*, not a template/context.
> It tells the user's browser to request a different URL so that they
> can't submit the form twice.
> --
> DR.
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Best way for making a "Poll"?

2008-11-16 Thread killsto

First of all, I know the tutorial supplies somewhat of a poll, and
I've been through it.

What I want to do is make an app that has one poll (or question) of
the day that someone can edit from the admin interface. Each question
has 4 choices. Also, there will not be user registration, just a field
to input your name. I also want to use ModelForm.

Now, should I have each question have a ManyToMany with choices, or
each choice have a ForeignKey with a question? Initially I thought a
ManyToMany would be good since its widget is multiple choice field in
ModelForm. But, it also showed up as a multiple choice (with no
choices yet!) in the admin interface.

So I decided to go with the way the tutorial had it setup, but I got
some error about a missing column when I tried to save a choice. (I'm
using sqlite3 and the SVN version) It also did that when I tried to
have it stacked inline with question. I tried flushing, resyncing the
db, and using django-evolution (after it didn't work). So I'm deciding
to start all over.

How can I get choices, a CharField I guess, to show up as radio select
widgets in a form?
Also, at the bottom of the page I want to show each (in standard text)
Question of the day and its choices. I'm assuming that's just simply
reading CharFields and displaying them?
--~--~-~--~~~---~--~~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Manipulating post data before sending?

2009-07-10 Thread killsto

I'm making a small app which is kind of like a blog. I have an Entry
class which has "ForeignKey(User)" as one of its members. I'm using
ModelForm to generate the form and I'm excluding the User ForeignKey
because it will just ask for you to pick a user from a drop-down
list.

Where do I tell it that the User ForeignKey should be request.user?
I've tried using "initial = {'author'  : request.user}". That
generates an "author_id may not be null." Any ideas?


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Manipulating post data before sending?

2009-07-10 Thread killsto

Thanks, that worked.

On Jul 10, 1:44 pm, Jonathan Buchanan 
wrote:
> > I'm making a small app which is kind of like a blog. I have an Entry
> > class which has "ForeignKey(User)" as one of its members. I'm using
> > ModelForm to generate the form and I'm excluding the User ForeignKey
> > because it will just ask for you to pick a user from a drop-down
> > list.
>
> > Where do I tell it that the User ForeignKey should be request.user?
> > I've tried using "initial = {'author'  : request.user}". That
> > generates an "author_id may not be null." Any ideas?
>
> Use the "commit" argument to the ModelForm's save() method to get a
> hold of the resulting model instance without saving it, make any
> changes you want to the instance and then call save() on it yourself.
>
> There's an example of this in the docs:
>
>    http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#the-sav...
>
> Regards,
> Jonathan.
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---