Hi,
I know newforms are still in development, but maybe there are known
practiced on
how to manage custom Form.
Let's say there's a model:
ITEM_TYPE_CHOICES = (
('new','new'),
('used','used')
)
class Item(models.Model):
owner = models.ForeignKey(Users) (maybe this should not be here
so it's not displayed at all?)
name = models.CharField(blank=False, maxlength="20")
type = models.CharField(maxlength="4", blank=False)
Here's the class Form:
class AddEditItemForm(forms.Form):
owner = ChoiceField(choices=[('','-------')]+[(o.id,o.email) for
o in User.objects.all()])
name = CharField()
type = CharField()
(I have ommited maxlength etc. for simplicity).
How shold the create/update view look like?
I would like to use {{ form.owner }} , {{ form.name }} in my template.
Some are using:
def create_edit_item(request, object_id):
if object_id is None:
item = Item.objects.get(id=object_id)
ItemForm = AddEditForm(initial=item.__dict__)
else:
ItemForm = AddEditForm()
problem 1: item.__dict__ returns a dict with: owner_id value instaed of
owner, initial won't work then
if using owner_id in the AddEditForm() it looks messy in the template
(name=id_owner_id). I know form_for_instance produces form with
"owner" field name, not owner_id.
I know there are some problems with form_for_model.save() yet.. and
that I have to manipulate form.clean_data and use object.save()
instaed, but how should my view If I want just edit name & type so that
the user
won't be asked to enter / won't be able to POST "owner" value.
Can anybody please explain how should my add/edit view & form look like
assuming the newly created item owner
gets the request.user & that when editing this can't be changed by
anyone?
Or maybe I can use form_for_instance, but I have no idea how can I "cut
off" some fields so they are not
in this form. That's just a thought.
I've considered the example from:
http://groups.google.com/group/django-users/browse_frm/thread/d9e03cf29739869f/71c60c65387802bc?lnk=gst&q=newforms&rnum=8#71c60c65387802bc
but I'd rather avoid using hiddenfields. Also I need to use {{
form.name }} instead of {{ form }}.
Thanks,
Robert
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django
users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---