Задать вопрос

Тел: +7 965 3737 888





PK->objects in view signature

<p>Let's suppose you have a view that fetches an object and renders a template with it. </p>
def show_blog_post(request, post_id):
    post = Post.objects.get(pk=int(post_id))
    return render_to_response("show_post.html", dict(post=post))

<p>That's all well and good. This decorator just lets you move one
statement into what is essentially a declaration at the top of the
function: "this is a view, that gets passed a primary-key ID, which I
want to get the Post object for."</p>
@sigtransform([Post, int])
def show_blog_post(request, post):
    # post is actually the Post instance now!
    return render_to_response("show_post.html", dict(post=post))

<p>If you want to leave an argument alone, pass None as that positional
argument (excluding request.)</p>
@sigtransform(None, [MyModel, str]):
def show_mymodel_stuff(request, leave_me_alone, model_obj):
    # ...etc...

<p>Internally, this just calls get_object_or_404 with the pk kwarg, which
means it's not limited to integer keys. However, Model.objects.get
requires that the pk arg be of the correct type, hence why the
arguments are 2-element lists or tuples: the first is the actual
model, the second is what cleaner function to apply to the passed in

Вопрос полезен? Да0/Нет0