models.py
class Person(models.Model):
userID = models.ForeignKey(User, unique=True)
name = models.CharField('name', max_length=200)
birthday = models.DateField('Birthday',blank=True,null=True)
gender = models.CharField(max_length=1, choices=gender_list)
email = models.EmailField('Email', max_length=100,unique=True)
favoriteURL = models.URLField('myURL')
desc = models.TextField('Desc', max_length=500,null=True)
friends = models.ManyToManyField('self', blank=True)
blogs = models.ManyToManyField(Blog, blank=True)
def __str__(self):
return '%s' % (self.name)
views.py
def person_form(request, pID='0'):
#default
**PersonForm = forms.form_for_model(Person)** my query
f = PersonForm()
message = 'Unknown Request'
p = get_object_or_404(Person, pk=pID)
if request.method == 'GET':
**PersonForm = forms.form_for_instance(p)** my query
message = 'Editing person %s ' % p.name
if request.method == 'POST':
if request.POST['submit'] == 'update':
message = 'Update Request for %s. ' % p.name
**PersonForm = forms.form_for_instance(p)** my query
if f.is_valid():
message += 'Valid.'
else:
message += 'Invalid.'
return render_to_response('People/person_for开发者_运维技巧m.html', locals())
I tried: PersonForm = ModelForm(model=Person)
but it throws __init__() got unexpected kewyword argument 'model'
. I'm new in Python and I use Django in 24 hours textbook it seems like most of the methods are deprecated. Any idea how can I can replace forms_for_model() with ModelForm()?
Django in 24 hours is really outdated now.
Consider switching to djangobook v2.0 - it's up to date and really great. And official docs on modelforms
Here is small comparison of old and newforms.
The docs explain this fully.
精彩评论