I modified my code to use forms.ModelForm
so that I can modify the width of the forms for the webpage. The problem I have now is that ForeignKeys
is not available for forms.Form and I need to save the active user in the form, as well as the current time when the request is submitted.
Below are my files (I excluded all necessary imports) with the current workaround, and it gives me the following error: table chemrun_chemrun has no column named owner_id
. I am happy for any help to solve this :)
from models.py
class ChemRun(models.Model):
owner = models.ForeignKey('auth.User')
from forms.py
class ChemRunForm(forms.ModelForm):
title = forms.CharField(max_length=50)
status = forms.CharField(max_length=20, initial="In queue")
created_date = forms.DateTimeField(initial=timezone.now, required=False)
def __unicode__(self):
return self.title
class Meta:
model = ChemRun
exclude = {'created_date', 'status', 'owner'}
from views.py
@verified_email_required
def create(request):
if request.POST:
form = ChemRunForm(request.POST)
if form.is_valid():
m = form.save(commit=False)
m.created_date = timezone.now
m.owner = request.user
m.save()
return HttpResponseRedirect('/accounts/profile')
else:
form = ChemRunForm()
args = {}
args.update(csrf(request))
args['form'] = form
return render_to_response('interface/newrun.html', args, context_instance=RequestContext(request))
from urls.py
urlpatterns = [
url(r'^create/$', 'chemrun.views.create', name='create'),
]