python - Django - forms vs model form, editing a model.textField, and getting the pk of an object? -
models.py
from django.db import models django import forms class history(models.model): commenttypes = ( ('1', 'issues'), ('2', 'risks'), ('3', 'dependencies'), ('4', 'accomplishments'), ('6', 'ugm') ) historyid = models.integerfield() projectid = models.integerfield() userid = models.charfield(max_length=6) logcomments = models.textfield(max_length=1000) timestamp = models.datetimefield(auto_now_add=true, auto_now=false) commenttype = models.charfield(max_length=1,choices=commenttypes)
forms.py
class statustab(modelform): class meta: model = history fields = ['logcomments']
views.py
def projecttabs(request): form = statustab(request.post or none) context = { "form":statustab, } if request.method == 'post' , form.is_valid(): instance = form.save(commit=false) #instance.historyid = instance.object_instance.pk instance.projectid = 1337 instance.userid = request.user # instance.commenttype instance.save() return render(request, "projects/project.html", context)
so question is, want able create textbox user enter information project. except when use models.textfield
keeps putting logcomments: *textbox*
, don't want "logcomments" title. have tried looking @ documentation , there doesn't seem gives indication of how edit this.
which leads me other part of question, difference when using forms.somefield
models.somefield
because forms seems have ton more customization. using mysql database wanted use model form frustratingly non-customizable , curious see if missing something.
also, finally, how pk id of object instance. want comments logged unique key figured why not correspond pk in db. except no matter google search perform cannot find method return pk.the closest found along lines of instance.object_instance.pk doesn't work.
any guidance on great. django documentation seems lacking when comes model forms.
your question touches on lot of different things, i'll try answer them.
see docs on rendering form fields manually. if don't want label displayed, leave out.
in form, should use form fields (e.g. forms.charfield
) , not model fields (e.g. models.charfield
). true model forms well. model fields should used when define model class.
you can instance's primary key instance.pk
. note can't until after have saved instance. @ time call form.save(commit=false)
, instance has not been saved database, doesn't have primary key yet.
Comments
Post a Comment