I am writing a simple task management app in Django. I am trying to
use the "create_object" generic view to allow a logged on user to
create a new Task. It does work, but the form displays a drop-down for
the Task's "user" field allowing the user to pick any user as the
task's owner. This is bad. I have solved that by setting
"editable=False" on my Task model.

How do I  automatically set the Task's "user" field to the logged on
user before saving the new Task? Can I use the create_object Generic
View or do I need to write a custom view?


Here's the specific section of the Django docs which is throwing me:
http://docs.djangoproject.com/en/dev/topics/forms/modelforms/?from=olddocs#using-a-subset-of-fields-on-the-form

===========================
"If you specify fields or exclude when creating a form with ModelForm,
then the fields that are not in the resulting form will not be set by
the form's save() method. Django will prevent any attempt to save an
incomplete model, so if the model does not allow the missing fields to
be empty, and does not provide a default value for the missing fields,
any attempt to save() a ModelForm with missing fields will fail. To
avoid this failure, you must instantiate your model with initial
values for the missing, but required fields, or use save(commit=False)
and manually set any extra required fields:

instance = Instance(required_field='value')
form = InstanceForm(request.POST, instance=instance)
new_instance = form.save()

instance = form.save(commit=False)
instance.required_field = 'new value'
new_instance = instance.save()
"
=================================

The Task model is as follows:

# models.py
class Task(models.Model):

    user = models.ForeignKey(User, editable=False)
    name = models.CharField(max_length=100)
    due = models.DateTimeField(blank=True, null=True)

# urls.py
task_dict = {'model': Task, 'login_required': True}

urlpatterns = patterns('',
    url(r'^create/$', create_object, task_dict)


Many thanks,
Doug
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to