การเขียนทับ get_queryset และเทมเพลต
ปรับแต่งข้อมูลและชื่อเทมเพลตสำหรับแต่ละวิว
การเขียนทับ get_queryset และเทมเพลต เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Django Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Make Generic Views Yours
Generic views give sensible defaults, but real apps need tweaks. You override a few hooks to customize them. 🔧
What get_queryset Does
get_queryset decides which rows a view works with, so overriding it lets you filter or sort the results.
Filtering with get_queryset
Return a filtered queryset to show only the rows you want, like published posts.
class PostList(ListView):
model = Post
def get_queryset(self):
return Post.objects.filter(published=True)Reading the URL or User
Inside get_queryset you can read self.request, so you can scope results to the logged-in user.
def get_queryset(self):
return Post.objects.filter(author=self.request.user)Override template_name
Do not like the default template path? Set template_name to point at any template you choose.
class PostList(ListView):
model = Post
template_name = "blog/home.html"Override the Context Variable
Pair template_name with context_object_name so your custom template reads with a friendly variable.
class PostList(ListView):
template_name = "blog/home.html"
context_object_name = "posts"Add Extra Context
Override get_context_data to pass extra values, like a page title, alongside the objects.
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
ctx["title"] = "Blog"
return ctxAlways Call super First
In get_context_data, call super() first so you keep the objects the view already added, then add your own.
Customize the Suffix
Prefer model_browse over model_list? Set template_name_suffix to change just the suffix Django appends.
class PostList(ListView):
template_name_suffix = "_browse"Order the Results
Override get_queryset to sort rows, for example showing the newest posts first.
def get_queryset(self):
return Post.objects.order_by("-created")Small Hooks, Big Control
With these few overrides you bend generic views to almost any need while keeping their tidy structure. 💡
Quick Check
What is the purpose of overriding get_queryset?
Recap: Customizing Views
You shaped generic views with get_queryset, template_name, and get_context_data, customizing data and templates without losing their simplicity. 🎉
คำถามที่พบบ่อย
บทเรียน “การเขียนทับ get_queryset และเทมเพลต” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การเขียนทับ get_queryset และเทมเพลต” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การเขียนทับ get_queryset และเทมเพลต”
ปรับแต่งข้อมูลและชื่อเทมเพลตสำหรับแต่ละวิว คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การเขียนทับ get_queryset และเทมเพลต” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม
ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ListView และ DetailView
- CreateView และ UpdateView
- DeleteView และ success_url
- การเขียนทับ get_queryset และเทมเพลต