django rest framework multiple url arguments
<p>In rest api view I need to have 2 objects. For example:</p>

<pre><code>class Foo(models.Model):
    ....

class Bar(models.Model):
    ....
</code></pre>

<p>What is correct way to get them? I mean how should I configure urls? I think this is not really good practice: <code>url(r'^foo/(?P&lt;pk&gt;\d+)/bar/(?P&lt;pk&gt;\d+)/$', FooBarView.as_view())</code>
Or: <code>url(r'^foobar/$', FooBarView.as_view())</code> and then pass parameters: <code>?foo=1&amp;bar=2</code>.</p>

<p>For django >= 1.5 You could pass parameters to class view doing somethin like this:</p>

<pre><code>class Foo(TemplateView):
    template_name = "yourtemplatesdir/template.html"
    # Set any other attributes here 

    # dispatch is called when the class instance loads
    def dispatch(self, request, *args, **kwargs):
        self.id = kwargs.get('foo_id', "other_def_id")
        self.barid = kwargs.get('bar_id', "other_def_id")

# depending on your definition, this class might be different 

class Bar(TemplateView):
    template_name = "yourtemplatesdir/template.html"
    # Set any other attributes here 

    # this method might not be necessary
    # dispatch is called when the class instance loads
    def dispatch(self, request, *args, **kwargs):
        self.id = kwargs.get('bar_id', "other_def_id")
</code></pre>

<p>In your url conf something like:</p>

<pre><code>from .views import FooBar

urlpatterns = patterns('',
    url(
        regex=r'^(?P&lt;foo_id&gt;\d+)/(?P&lt;bar_id&gt;\d+)/$'
        view=FooBar.as_view(),
        name='viewname'
    ),
)
</code></pre>

<p>Then, in your template view for FooBar, you could do:</p>

<pre><code>{{ view.id }}
{{ view.barid }}
</code></pre>

<p>I hope it helps, it could be more detailed if you provide further information about the object information needed in your '<em>action</em>'.</p>

python django django-rest-framework