On Thu, Jun 17, 2010 at 8:00 PM, Waldemar Kornewald
<wkornew...@gmail.com> wrote:
> Imagine what the code would look like with parameter passing:
>
> class View(object):
>    def __call__(self, request, **kwargs):
>        qs = self.get_queryset(request, **kwargs)
>        .....
>
>    def get_queryset(self, request, **kwargs):
>        ...

Why would you hardcore the queryset inside the view? Pass the
pre-filtered queryset to the constructor and use get_objects_or_404 to
filter it basing on what is passed to __call__.

Actually as I see it __call__ is not the best idea:

class GenericView(object):
    def __init__(self, queryset):
        self.qs = queryset

    def details(self, request, id):
        obj = get_object_or_404(self.qs, id=id)
        return direct_to_template(request, 'details.html', {'object': obj})

    def list(self, request):
        objs = get_objects_or_404(self.qs)
        return direct_to_template(request, 'list.html', {'objects': objs})

    # ... and so on

> Now someone writes a reusable view for S3 file management which only
> has one parameter (bucket_name) and publishes the code as an
> open-source project:
>
> class S3View(View):
>    def get_queryset(request, bucket_name, **kwargs):
>        self.get_bucket(bucket_name)
>        ...
>    ...

Are you sure you need to pass the bucket name each time you _call_ the
view? I'd pass it at construction time instead.

> Then you want to use his code in your project:
>
> class MyS3View(S3View):
>    def get_bucket(self, bucket_name):
>       # oh no! I need the request and the other configuration
> paramters here! :(

Request sure, but what other configuration parameters? Default
implementation should under no circumstances pass and silently swallow
**kwargs.

> Now you have a problem because the other developer was too lazy to
> pass request and **kwargs to that little "unimportant" get_bucket
> method. This happens in practice and our job is to come up with a
> design that makes this impossible or very unlikely. If the
> one-instance-per-request solution solves this and saves you from
> typing **kwargs all over the place then it absolutely is better, hands
> down.

See above.

-- 
Patryk Zawadzki

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

Reply via email to