Pierre de LESPINAY
Pierre de LESPINAY

Reputation: 46158

Django - Foreign key reverse creation

Imagine this model:

class ExercisePart(models.Model):
  exercise = models.ForeignKey(Exercise)
  sequence = models.IntegerField()
  class Meta:
    unique_together = (('exercise', 'sequence',),)

class Exercise(models.Model):
  name = models.CharField(max_length=15)

From the admin interface I'd like to be able to create/link ExerciseParts through the Exercise page. I'd like to do that because I wish to avoid having to go on another page each time I want to add an ExerciseParts.

Is it possible ? How can I do that ?

Upvotes: 0

Views: 468

Answers (1)

Thibault J
Thibault J

Reputation: 4446

You're looking for the inline admin feature.

admin.py

class ExercisePartInline(admin.TabularInline):
  model = ExercisePart
class ExerciseAdmin(admin.ModelAdmin):
  inlines = [ExercisePartInline]

Upvotes: 2

Related Questions