Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

627
Views
How do use Foreign Key in slug django

My Models:

class Faculty(models.Model):
    name = models.CharField(max_length=30)


class Program(models.Model):
    name = models.CharField(max_length=30)
    faculty = models.ForeignKey(Faculty)


class Student(models.Model):
    name = models.CharField(max_length=30)
    slug = models.SlugField(max_length=30, unique=True)
    faculty = models.ForeignKey(Faculty)
    program = models.ForeignKey(Program)

my Views

def profile(request, slug, faculty, program):
    template_name = 'profile.html'
    infor = get_object_or_404(Candidate, slug=slug, faculty=faculty, program=program)
    context = {'title': infor.name}
    return render(request,template_name,context)

Urls

url(r'^(?P<faculty>[\w-]+)/(?P<program>[\w-]+)/(?P<slug>[\w-]+)/$', profile, name='profile'),

Now I got the profile at host/1/1/sagar-devkota/ what I need is host/science/be/sagar-devkota/ Let assume science is a faculty and be is a program.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

give slug field to both faculty and program model. And in filter use __ for related lookups.

infor = get_object_or_404(Candidate, slug=slug, faculty__slug=faculty, program__slug=program)

you can do it by using with that name field too.

infor = get_object_or_404(Candidate, slug=slug, faculty__name=faculty, program__name=program)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!