Django জ্যাঙ্গো
  • Python Basic
  • Python OOP
  • OOP Project
  • Jupyter Notebook
  • Python MOngodb
  • Numpy
  • Pandas
  • Framework কি?
  • জ্যাঙ্গো ওয়েব ফ্রেমওয়ার্ক
  • ভার্চুয়াল এনভায়র্নমেন্ট
    • ভার্চুয়াল এনভায়র্নমেন্ট তৈরী করি :
    • জ্যাংগো ইন্সটল
  • প্রথম প্রজেক্ট শুরু
    • সেটিংস পরিচিতি
    • Template
      • load css,js
        • template load
    • create project
  • অ্যাপস কি?
    • প্রথম অ্যাপস তৈরি
  • apps কে প্রজেক্টের সাথে যুক্ত করি
  • এইচটিএমএল টেমপ্লেট নিয়ে কাজ করি
    • স্ট্যাটিক ফাইল ও টেম্পলেট
    • Hompage পরিবর্তন করি
    • বেস টেমপ্লেট বানাই
    • html টেম্পলেট কে কনভার্ট করি
    • Template Tags
    • Context Processor
  • Urls
  • urls.py এ প্যারামিটার কিভাবে কাজ করে
  • message
  • Forms
    • সাধারণ একটি ফর্ম বানাই
    • ফর্ম লে আউট পরিবর্তন
    • বুটস্ট্রাপ ফর্ম
    • মডেল ফর্ম
      • Form Customization
  • models
    • মডেল বানাই
    • মাইগ্রেশন
    • মডেল ভ্যালিডেশন করা
    • মডেল কাস্টমাইজ
  • Crud
    • image Crud Admin Panel
    • Custom Crud
      • form design bootstrap
      • search Functionality
      • menu
    • File Upload App
    • Image Crud
    • Class view Crud
  • Django Authentication
    • Login/Logout(Default)
    • Registration Page(Default )
    • Profile UpdateView (Default)
    • Change Password (Default)
    • Custom Login And Registration
    • Protecting View
  • Custom Field User Model
  • Formset
    • Page 1
    • formset Crud
  • Send Email
    • Send Email From Gmail
      • Send Email With Attachment
    • Email Verification
  • Uploading Images to Cloudinary
  • Database Connection
    • MySQL
    • postgresql
  • Django Orm
    • Basic Database Operation
      • -all()
      • - get():
      • - first() and last():
      • - filter():
      • - exclude():
      • Chaining Queries
      • Creating, Updating, and Deleting Records
      • Aggregation
      • Annotation
    • Model Relationships and Related Names
      • One-to-One Relationships
      • Many-to-One Relationships
      • Many-to-Many Relationships
      • Reverse Relationships
      • Understanding related_name
    • Advanced Querying with Django ORM
      • Chaining Queries
      • F() Expressions
      • Q() Objects
        • Dynamic Search
      • Raw SQL Queries
    • Performance Optimization with Django ORM
      • Select Related
    • Django সিগন্যাল :
  • requirement.txt
  • channels
    • Create Project
    • কনফিগার
  • Send SMS in Django
    • Configuration
  • Mysql Setup
  • অ্যাডমিন প্যানেলে
    • Site Heading And Title Change
    • মডেলকে অ্যাডমিন প্যানেলে যোগ করি
    • মডেলকে একবার সেভ করা যাবে
    • এডমিন প্যানেলে পিডিএফ এক্সপোর্ট বাটন যোগ করি
    • মডেল ফর্ম কাস্টোমাইজ করি
    • মডেলের html টেম্পলেট পরিবর্তন করি
    • ফিল্ড এর ডিজাইন বুটস্ট্রাপ ফরম্যাটে হবে
    • টেবিল বেসড ফর্মসেট এপ্লিকেশন
    • নির্দিষ্ট ইউজারকে মডেলে এক্সেস পারমিশন দেয়া
    • অ্যাডমিন প্যানেলে জাভাস্ক্রিপ্ট ও css ফাইল যোগ করা
    • fetch data
    • bill of materials
  • Django Rest Framework
    • ভার্চুয়াল এনভায়র্নমেন্ট
    • প্রজেক্ট তৈরী করি
    • মডেলকে প্রস্তুত করি
    • serializer
    • Validation
    • Api তৈরী করি (api_view)
    • Api তৈরী করি (viewsets)
    • JsonResponse তৈরি করি
    • authentication
    • Reactjs
  • প্যাকেজ নিয়ে কাজ করি
    • Tinymce Editor
    • highlight.js
    • Social Login
      • Facebook
      • Github
      • google
    • Select2
      • admin panel
    • CELERY
    • Autocomplete Show Multiple Fields
      • Autocomplete Search
  • Mongodb
    • basic models,forms,views
    • Category Crud
      • Category List
      • Create Category
      • Edit Category
      • Delete Category
      • Link Button list.html
    • Tags Crud
      • Tags List
      • Create Tags
      • Edit Tags
      • Delete Tags
      • Link Button list.html
    • Post
      • Post List
      • Create Post
        • Tinymc
      • Post Details
      • Edit Post
      • Delete Post
      • Link Button list.html
      • Search
    • Comment
      • Post Details
    • Formset Crud
      • Models.py
      • forms.py
      • views
      • urls.py
      • Author
        • author_list
        • create_author
        • Edit Author
        • Delete Author
        • Link Button
      • BookFormset
    • Rest Api
    • Image Upload
  • Redis
  • chart
  • Deployment
    • Pythonanywhere
    • Cpanel Host
  • Reactjs
Powered by GitBook
On this page
  1. Django Orm
  2. Model Relationships and Related Names

Many-to-One Relationships

Create the Post and Comment models:


from django.db import models

class Post(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()

class Comment(models.Model):
    post = models.ForeignKey(Post, on_delete=models.CASCADE)
    text = models.TextField()
    date_created = models.DateTimeField(auto_now_add=True)

In this example:

We have two models: Post and Comment. Each Post can have multiple Comment objects associated with it, creating a Many-to-One relationship. The Comment model has a foreign key field post that establishes the relationship to the Post model. The on_delete=models.CASCADE argument means that if a Post is deleted, all its associated comments will be deleted as well. We also have a text field to store the content of the comment and a date_created field to store the creation date.

Create and retrieve Post and Comment objects:

# Create a new post
post = Post.objects.create(title="Sample Post", content="This is the content of the post.")

# Create comments related to the post
comment1 = Comment.objects.create(post=post, text="First comment on the post.")
comment2 = Comment.objects.create(post=post, text="Second comment on the post.")

# Retrieve comments for a specific post
comments_for_post = Comment.objects.filter(post=post)

# Access the related post for a comment
for comment in comments_for_post:
    print(f"Comment: {comment.text}")
    print(f"Posted on: {comment.date_created}")
    print(f"Related Post: {comment.post.title}\n")

We first create a Post and two related Comment objects. Each comment is associated with the post it relates to. We then retrieve all comments related to a specific post using Comment.objects.filter(post=post). To access the related Post for each comment, we use the comment.post attribute, which gives us access to the associated Post object. This example demonstrates a Many-to-One relationship in Django between the Post and Comment models. It allows you to associate multiple comments with a single post and easily access them through the foreign key relationship.

PreviousOne-to-One RelationshipsNextMany-to-Many Relationships

Last updated 1 year ago