[Django]-How to reference a slug from a different model in get_success_url?

4👍

You can just follow the relationship:

def get_success_url(self):
    return reverse('to-journals', kwargs={'slug': self.object.journal_name.slug})

2👍

Your self.object is a to_journal_entry. You probably want to use the to_journal, you can do that by obtaining the journal_name:

class ToJournalEntriesList(LoginRequiredMixin, CreateView):
    model = to_journal_entry
    template_name = 'to_journals/to_journal_entries_list.html'
    fields = ('body',)

    def get_success_url(self):
        return reverse('to-journals', kwargs={ 'slug': self.object.journal_name.slug })

Note: usually the names of the models are written in PerlCase, so JournalEntry instead of to_journal_entry.

Leave a comment