There's a much easier way to do this, and no extra features are required:
class ItemCreateView(CreateView):
form_class = ItemForm
def form_valid(self, form):
form.instance.owner = self.request.user
return super(MyView, self).form_valid(form)
> Another question, or I'd rather say remark, is what Mixins are awesome
> and seem better than decorators. My views.py file looks like this now:
...
> and I like this "style" more than dancing with old-school decorators
> in one way or another. We've already had this conversation, but I
> can't still can't see neither why this style is bad nor how should I
> write it instead? (By "bad" and "should write" I mean "not supported
> and not encouraged by Django" and "supported and encouraged by
> Django").
Well, Mixins predate decorators by some time; Mixins are really just a
pattern of multiple inheritance.
It's not a matter of one being better than the other, either. They are
for solving different problems. Decorators allow you to wrap an
existing block of logic with entry and exit conditions. Mixins allow
you to compose complex behaviors out of smaller primitives, and
enhance internal behaviors by replacing key components.
In some cases, you will be able to get the same outcome using either
approach; it's really up to you as to which one you want to use.
Yours
Russ Magee %-)
--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To post to this group, send email to django-d...@googlegroups.com.
To unsubscribe from this group, send email to django-develop...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.
I didn't say self.instance. I said *form*.instance.
> Well, Django can provide tools for extending views with either decorators or
> mixins (or both - won't it cause uncertainity? maybe with some
> recommendation). Like the one from my example,
> django.contrib.auth.decorators.login_required can be transformed into Mixin.
> Will it be?
Unlikely, because login is a natural fit for a decorator.
You have a view. The operation of that view can be a black box with
regards to login. The decorator can check login status, and redirect
if you're not logged in; or call the black box if you are.
Sure, you *could* implement this as a mixin, but I'm not sure I see
the gain in offering two ways to do the same thing.
Yours,
Russ Magee %-)
Yours,
Russ Magee %-)
You can see my clumsy implementation of a CBV decorator[1]. This lets you write:
@view_decorator(login_required)
class ProtectedView(View):
pass
class MyView(ProtectedView):
pass
and the view function produced by MyView.as_view() will also have
login_required applied to it. You can use a simillar technique to
transform a function decorator to a Mixin, but class decorators are
more readable IMHO.
[1]: https://github.com/lqc/django/blob/cbvdecoration_ticket14512/django/utils/decorators.py#L42
PS. I guess I need to cleanup that patch so It could make it to 1.3 ;)
--
Łukasz Rekucki
For the record - I'm still interested in getting CBV decorators into 1.3.
Yours,
Russ Magee %-)
--