On Nov 9, 2007 12:42 PM, Joseph Kocherhans <[EMAIL PROTECTED]> wrote:
> Why not just do this? No need for any special new fields at all.
>
>     class Article(models.Model):
>         author = models.ForeignKey(User)
>         title = models.CharField(max_length=255)
>         body = models.TextField()
>
>     class ArticleForm(ModelForm):
>         model = Article
>         exclude = ['author']
>
>     def new_article(request):
>         article = Article(author=request.user)
>
>         if request.method == 'POST':
>             form = ArticleForm(request.POST, obj=article)
>             if form.is_valid():
>                 obj = form.save()
>                 ...
>         else:
>             form = ArticleForm(obj=article)
>
> It's the business of the form to know what the user is supposed to
> provide. In this case, author needs to be provided by the programmer,
> and if the programmer screwed up and saving the Article raises an
> error due to a missing author field, there's nothing the user can do.
> Adding extra machinery to handle stuff like that in the form seems
> overly complicated to me.

True. The only potential advantage of my approach over yours is the
ability to display information about the author in the template,
alongside the other forms, but if that's really necessary, the author
can be passed separately in the context. I hadn't considered just
creating a new object preloaded with the necessary information and
treating it as an incomplete instance form. That's quite reasonable.

I'm not sure about the "exclude = ['author']" syntax though. It seems
to me that setting "author" to None would make more sense, rather than
having a reserved name ("exclude") that takes a list of strings. Part
of the reason I proposed some Form.for_model(Article) syntax for the
base class was to eliminate the need for a reserved name ("model" or
"Meta") just for handling this process. Someone putting together a
form for a Lego set should be able to use "model" as a field name
without a problem, just like somebody writing an email form that mails
to a distribution list should be able to use "exclude" as a field for
addresses to exclude from delivery.

-Gul

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to