django - create a model that lets you insert multiple values for the same field?

后端 未结 1 1507
礼貌的吻别
礼貌的吻别 2020-12-10 06:02

Ok I\'m trying to do something which should be very simple in my mind but I am probably missing some SQL or django admin knowledge to get to it. Say I have a simple model s

相关标签:
1条回答
  • 2020-12-10 06:23

    Create a Review model which holds the review text and has a ForeignKey to Book...

    class Book(models.Model):
       title = models.CharField()
    
    class Review(models.Model):
       book = models.ForeignKey(Book, related_name='reviews')
       review = models.TextField()
    

    ...then register the appropriate type of InlineModelAdmin to edit all the related reviews on the Book's page in the admin. I'd suggest using a StackedInline in this case:

    class ReviewInline(admin.StackedInline):
        model = Review
    
    class BookAdmin(admin.ModelAdmin):
        inlines = [
            ReviewInline,
        ]
    

    The documentation has an example of almost this exact scenario, except for multiple authors instead of multiple reviews:

    • http://docs.djangoproject.com/en/dev/ref/contrib/admin/#inlinemodeladmin-objects
    0 讨论(0)
提交回复
热议问题