[Django]-How to use updateview with a ForeignKey/OneToOneField

6👍

You can use the slug_field and slug_url_kwarg attributes for this:

url(r'^moderate/(?P<issue_id>\d+)', ModEdit.as_view(),name='moderation')

class Modedit(UpdateView):
    slug_field = 'issue_id'
    slug_url_kwarg = 'issue_id'
    model = ModTool
    template_name = 'myapp/moderate.html'
    fields = ['priority','status']

This will do a lookup on issue_id=<issue_id> where issue_id is the issue’s primary key as captured in the url.

I’ve renamed the keyword argument pk to issue_id to prevent a name clash with the lookup for the primary key. Otherwise an additional filter would take place that filtered on the ModTool‘s primary key with the value for the Issue‘s primary key.

👤knbk

Leave a comment