👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
How can I create more than one ModelAdmin for the same model, each customised differently and linked to different URLs?
Let"s say I have a Django model called Posts. By default, the admin view of this model will list all Post objects.
I know I can customise the list of objects displayed on the page in various ways by setting variables like list_display or overriding the queryset
method in my ModelAdmin like so:
class MyPostAdmin(admin.ModelAdmin):
list_display = ("title", "pub_date")
def queryset(self, request):
request_user = request.user
return Post.objects.filter(author=request_user)
admin.site.register(MyPostAdmin, Post)
By default, this would be accessible at the URL /admin/myapp/post
. However I would like to have multiple views/ModelAdmins of the same model. e.g /admin/myapp/post
would list all post objects, and /admin/myapp/myposts
would list all posts belonging to the user, and /admin/myapp/draftpost
might list all posts that have not yet been published. (these are just examples, my actual use-case is more complex)
You cannot register more than one ModelAdmin for the same model (this results in an AlreadyRegistered
exception). Ideally I"d like to achieve this without putting everything into a single ModelAdmin class and writing my own "urls" function to return a different queryset depending on the URL.
I"ve had a look at the Django source and I see functions like ModelAdmin.changelist_view
that could be somehow included in my urls.py, but I"m not sure exactly how that would work.
Update: I"ve found one way of doing what I want (see below), but I"d still like to hear other ways of doing this.
👻 Read also: what is the best laptop for engineering students?
Multiple ModelAdmins/views for same model in Django admin __del__: Questions
How can I make a time delay in Python?
5 answers
I would like to know how to put a time delay in a Python script.
Answer #1
import time
time.sleep(5) # Delays for 5 seconds. You can also use a float value.
Here is another example where something is run approximately once a minute:
import time
while True:
print("This prints once a minute.")
time.sleep(60) # Delay for 1 minute (60 seconds).
Answer #2
You can use the sleep()
function in the time
module. It can take a float argument for sub-second resolution.
from time import sleep
sleep(0.1) # Time in seconds
Multiple ModelAdmins/views for same model in Django admin __del__: Questions
How to delete a file or folder in Python?
5 answers
How do I delete a file or folder in Python?
Answer #1
os.remove()
removes a file.os.rmdir()
removes an empty directory.shutil.rmtree()
deletes a directory and all its contents.
Path
objects from the Python 3.4+ pathlib
module also expose these instance methods:
pathlib.Path.unlink()
removes a file or symbolic link.pathlib.Path.rmdir()
removes an empty directory.
We hope this article has helped you to resolve the problem. Apart from Multiple ModelAdmins/views for same model in Django admin, check other __del__-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano Multiple ModelAdmins/views for same model in Django admin
- Deutsch Multiple ModelAdmins/views for same model in Django admin
- Français Multiple ModelAdmins/views for same model in Django admin
- Español Multiple ModelAdmins/views for same model in Django admin
- Türk Multiple ModelAdmins/views for same model in Django admin
- Русский Multiple ModelAdmins/views for same model in Django admin
- Português Multiple ModelAdmins/views for same model in Django admin
- Polski Multiple ModelAdmins/views for same model in Django admin
- Nederlandse Multiple ModelAdmins/views for same model in Django admin
- 中文 Multiple ModelAdmins/views for same model in Django admin
- 한국어 Multiple ModelAdmins/views for same model in Django admin
- 日本語 Multiple ModelAdmins/views for same model in Django admin
- हिन्दी Multiple ModelAdmins/views for same model in Django admin
Massachussetts | 2023-03-24
I was preparing for my coding interview, thanks for clarifying this - Multiple ModelAdmins/views for same model in Django admin in Python is not the simplest one. I just hope that will not emerge anymore
Berlin | 2023-03-24
Maybe there are another answers? What Multiple ModelAdmins/views for same model in Django admin exactly means?. Will use it in my bachelor thesis
Warsaw | 2023-03-24
Maybe there are another answers? What Multiple ModelAdmins/views for same model in Django admin exactly means?. I am just not quite sure it is the best method