1

Ich habe diese Modelle, bei denen ein Buch viele Inhalte in verschiedenen Sprachen haben:Django REST-Framework - get verwandte Objekte

class Book(models.Model): 
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) 
    title = models.CharField(max_length=255) 

class BookContent(models.Model): 
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) 
    book = models.ForeignKey(Book, on_delete=models.CASCADE) 
    content = models.TextField() 
    language = models.TextField() 

----------------- --- Update --------------------

Wie soll ich Book und seine verwandte BookContent so holen, dass das Ergebnis JSON wie folgt aussieht?

{ 
    "results": [ 
     { 
      "id": "d3e5185a-1b7b-427c-bbe3-030bfa2e3bce", 
      "title": "My Book Title",    
      "book_content": [ 
       { 
        "id": "0fea8027-3ecf-4571-a95f-5a09a93408ec", 
        "content": "hello content 1", 
        "language": "english" 
       }, 
       { 
        "id": "0fea8027-3ecf-4571-a95f-5a09a93408ed", 
        "content": "你好", 
        "language": "chinese" 
       } 
      ] 
     } 
    ] 
} 

Antwort

1

benötigen Sie eine Abfrage für das wie

take a look at doc Referenz schreiben

query = BookContent.objects.filter(book=book_id)# this will give 
 
you a queryset with all the book content records with that particular book_id

+0

Ihnen danken. aber wie hole ich stattdessen mit Book-Objekt? – nuttynibbles

+0

Um ein einzelnes Objekt zu erhalten, benutzen Sie get() zB. Book.objects.get (id = book_id) –

1

Sie müssen sich mit bestimmten bookid oder booktitle alle Inhalte dieser particular book zu bekommen, was Sie habe in url verwendet.

bookcontents = BookContent.objects.filter(book__id=bookid) 

oder wenn Sie booktitle in Ihrem url dann verwendet haben.

bookcontents = BookContent.objects.filter(book__title=booktitle) 

Jetzt in html

{% for cont in bookcontents %} 
    <h1>{{bookcontent.book.title}}</h1> 
    <b>{{bookcontent.language}}</b> 
    <p>{{bookcontent.content}}</p> 
{% endfor %} 

Wenn nur Buchobjekte bekannt ist.

book = get_object_or_404(Book, id=bookid) 

Jetzt in html

{% for cont in book.bookcontent.all %} 
    <h1>{{cont.book.title}}</h1> 
    <b>{{cont.language}}</b> 
    <p>{{cont.content}}</p> 
{% endfor %} 
+0

Hallo danke. Aber wie bekomme ich stattdessen ein Buchobjekt? – nuttynibbles

+0

Warte, ich aktualisiere die Antwort dafür. –

+0

@nuttynibbles, ich habe meine Antwort aktualisiert. –

1

models.py
In BookContent, fügen related_name zu foreignKey Buch

class Book(models.Model): 
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) 
    title = models.CharField(max_length=255) 

class BookContent(models.Model): 
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) 
    book = models.ForeignKey(Book, on_delete=models.CASCADE, related_name='content') 
    content = models.TextField() 
    language = models.TextField() 

serializers.py
Siehe für weitere Informationen here

class BookContentSerializer(serializers.ModelSerializer): 
    class Meta: 
     model = BookContent 
     fields = ('id', 'content', 'language') 

class BookSerializer(serializers.ModelSerializer): 
    content = BookContentSerializer(many=True, read_only=True) 

    class Meta: 
     model = Book 
     fields = ('id', 'title', 'content') 

Ergebnis:

{ 
    "results": [ 
     { 
      "id": "d3e5185a-1b7b-427c-bbe3-030bfa2e3bce", 
      "title": "My Book Title",    
      "book_content": [ 
       { 
        "id": "0fea8027-3ecf-4571-a95f-5a09a93408ec", 
        "content": "hello content 1", 
        "language": "english" 
       }, 
       { 
        "id": "0fea8027-3ecf-4571-a95f-5a09a93408ed", 
        "content": "你好", 
        "language": "chinese" 
       } 
      ] 
     } 
    ] 
} 
Verwandte Themen