Issue
So, I'm doing the classical movie app in Django (I'm loving the framework!) and I feel like I hit a stump.
So, I have my Movie model:
class Movie(models.Model):
TitlePRT = models.CharField(max_length=100)
TitleORG = models.CharField(max_length=100)
TitleAlt = models.CharField(max_length=100)
Poster = models.ImageField(upload_to=posterPath, max_length=100)
Synopsis = models.TextField()
Year = models.IntegerField()
DubStudio = models.ForeignKey("Studio", on_delete=models.CASCADE)
Trivia = models.TextField()
CreatedBy = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="+")
CreatedAt = models.DateTimeField(auto_now=False, auto_now_add=True)
Modifiedby = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="+")
ModifiedAt = models.DateTimeField(auto_now=True, auto_now_add=False)
My Actor model:
class Actor(models.Model):
Bio = models.TextField()
Name = models.CharField(max_length=50)
FullName = models.CharField(max_length=50)
DateOfBirth = models.DateField()
Country = models.TextField(max_length=100)
Nationality = models.TextField(max_length=50)
Debut = models.ForeignKey("Movie", on_delete=models.CASCADE)
CreatedBy = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="+")
CreatedAt = models.DateTimeField(auto_now=False, auto_now_add=True)
ModifiedBy = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="+")
ModifiedAt = models.DateTimeField(auto_now=True, auto_now_add=False)
ClaimedBy = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="+")
And, finally, my Cast model:
class Cast(models.Model):
Movie = models.ForeignKey("Movie", on_delete=models.CASCADE)
Actor = models.ForeignKey("Actor", on_delete=models.CASCADE)
Character = models.TextField(max_length=50)
Observations = models.TextField(max_length=50)
Right now, I can add content through Django admin, but it's awkward, because I can't add a cast in the Movie form. I have to go to the Cast form, and a character one by one
I've searched about customizing admin forms and whatnot, but I get a little confused.
Is there to create a form field in the Movie form to add the cast, preferably with three elements: one where you can choose the actor form the actors in the db, a text field to type the role and another text field to add a little observation?
Or is there a less convoluted way to organize this data?
Solution
Yes, you can create a TabularInline
[Django-doc] for the Cast
:
class CastInline(admin.TabularInline):
model = Cast
and then inject that into the ModelAdmin
for the Movie
:
class MovieAdmin(admin.ModelAdmin):
# …
inlines = [CastInline]
Answered By - willeM_ Van Onsem
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.