Here, by dynamic I mean, I wouldn't want to update my template in order for me to update changes. I'd want them to edit in the admin page on my production site. At first, I thought I'd create a model for "About Me" itself, but then I'd need to create a model for just one instance.
I need help with this, better ways to edit my pages dynamically on the admin site.
Perhaps you could create a model for About Me as you mentioned. Since you've highlighted that you would want to work with the django admin site, then what you could do is to set the permission for only one object to be created for that model which can be updated whenever.
For example:
models.py file.
class AboutMe(models.Model):
# With the desired fields of your choice
Now you can set the permission within the admin.py file to only allow one instance from the model to be created.
from django.contrib import admin
from .models import AboutMe
MAX_OBJECTS = 1
# Using decorator here
#admin.register(AboutMe)
class AboutMeAdmin(admin.ModelAdmin):
fields = ['..', '..', '..'] # fields you want to display on the forms
list_display = ['..', '..', '..'] # fields you want to display on the page for list on objects
# Allowing the user to only add one object for this model...
def has_add_permission(self, request):
if self.model.objects.count() >= MAX_OBJECTS:
return False
return super().has_add_permission(request)
That should be a nice fit for your situation. Also, you can read the docs to learn more about customizing django admin site.
Related
I have a model that I only want to use one row of its table. So, on admin, I would like to remove the list and add pages, and only edit the existing object. The model is this:
from django.db import models
class Banner(models.Model):
pass
class BannerImg(models.Model):
img = models.ImageField(upload_to="banners")
banner = models.ForeignKey(Banner, on_delete=models.CASCADE)
Basically, the Banner(pk=1) will be loaded by the frontend to display a landing page hero slider. I want multiple images, but also want them to be on the same admin form, so one could order, add or remove images from the same place. Of course having to Banner objects, wouldn't make sense in this case.
I can use inline fields to do the form, but how can I achieve the pages functionality (going directly to edit)?
Thanks!
Accordion to documentation.
from django.contrib import admin
class BannerImgInline(admin.TabularInline):
model = BannerImg
class BannerAdmin(admin.ModelAdmin):
inlines = [
BannerImgInline,
]
I have a model with fields name, roll_no, birth_date
I am using the django admin's list display and list editable to have these fields displayed and edited in a list format in a single page. However, to add a new entry I have to go to the create_form page.
Is it possible to simply add new objects from the list_display page itself?
Unfortunately this feature is not available out-of-the box in the Django admin like the ModelAdmin.list_editable feature.
I'm curious to see if there are other shortcuts, but at the moment the only way I see is to customize the formset like descibed in the official Docs:
from django import forms
class MyForm(forms.ModelForm):
# customize your 'extra' forms here
class MyModelAdmin(admin.ModelAdmin):
def get_changelist_form(self, request, **kwargs):
return MyForm
And finally manually extend the changelist form template of the admin. To override a Django admin template, please follow the intructions in the Official Docs here. The template to be customized is the following folder:
.../django/contrib/admin/templates/admin/change_list.html
and you probably need to override the {% block result_list %} in that file.
NB: the customization of an admin template can be very tricky. Consider to use a CMS (like DjangoCMS) if you need to extend the user experience. The idea behind the Django admin is to make your life easier with an out-of-the-box interface for CRUDs on your DB. IMHO try to avoid complex customizations of the Django Admin if not strictly needed.
Here's a piece of Django admin interface's instance edition form:
How should I change the underlying admin.ModelAdmin instance to make it contain an URL, like this?
Django makes this easy. Subclass ModelAdmin, add a custom method and then tell the Admin how to use it. Here's a sample admin.py:
from django.contrib import admin
from .models import Vendor
class VendorAdmin(admin.ModelAdmin):
readonly_fields = ['example_link']
def example_link(self, obj):
return 'link text'.format(obj.get_link()) # however you generate the link
example_link.allow_tags = True
admin.site.register(Vendor, VendorAdmin)
Here's the documentation that furthers explains readonly_fields, customizing the form label text with short_description, ordering, and how you can put this custom url method on the Model or ModelAdmin.
If you are on the django admin page for the model Group. You don't know that there is a reverse relation to user.
Some people (not me) have difficulties with it.
Is there a way to show all reverse relations, so that you can jump to the matching admin pages?
Example:
On admin page for Group I want a link to User (and all other models which refer to it).
This should happen by code, not by hand with templates.
This method doesn't automatically add links to all related models of a Group, but does for all Users related to a Group (so for one related model at a time). With this you'll get an inline view in your Group with the related Users.
You could probably extend this technique to make it automatically work for all related fields.
class UserInline(admin.StackedInline):
model = User
extra = 0
readonly_fields = ('change',)
def change(self, instance):
if instance.id:
# Django's admin URLs are automatically constructed
# based on your Django app and model's name.
change_url = urlresolvers.reverse(
'admin:djangoapp_usermodel_change', args=(instance.id,)
)
return '<a class="changelink" href="{}">Change</a>'.format(change_url)
else:
return 'Save the group first before editing the user.'
change.allow_tags = True
class GroupAdmin(admin.ModelAdmin):
list_display = ('name',)
inlines = (UserInline,)
You might also be interested in this extension I created for Django admin pages to link to related objects:
https://github.com/gitaarik/django-admin-relation-links
It's quite easy to use and makes the admin a lot more convenient to use :).
I have a table that only has a handful of entries in it, and it'd be nice if I could use inlines for their list instead of forcing staff to click through to the edit page each time.
That is, when someone clicks on the link that ordinarily gives a list of the model objects, they should instead see the model objects displayed inline.
I tried something like this, but unsurprisingly it gives an error because there's no foreign key:
class MyModelInline(admin.StackedInline):
model = MyModel
class MyModelAdmin(admin.ModelAdmin):
inlines = [MyModelInline,]
admin.site.register(MyModel, MyModelAdmin)
For it to work as you've described you'll need an "editor" model to be a parent for the data. All the rows you want to display should have a foreign key to a single 'editor' model object. So, in models.py:
from django.db import models
class Editor(models.Model):
pass
class MyModel(models.Model):
name = models.CharField(max_length=100) # Field added for demonstration
# ... add any other fields you like ...
editor = models.ForeignKey(Editor)
And in admin.py:
from django.contrib import admin
from Test.models import Editor, MyModel
class MyModelInline(admin.StackedInline):
model = MyModel
class EditorAdmin(admin.ModelAdmin):
inlines = [MyModelInline,]
admin.site.register(Editor, EditorAdmin)
Some other things to consider:
When you make a new MyModel() object programmatically you must always set the foreign key to point to the editor. There should only be one instance of the editor for this to work as you've described. When using the admin interface, this foreign key should be set automatically by using the admin page for the editor object. I would suggest restricting creation and deletion of editor objects for everyone except yourself in production. If someone deletes the editor object then all MyModel objects disappear as well.
Alternative options:
1) If the edits the admin staff is doing are simple then I would recommend implementing "actions" instead.
2) There's also the possibility of overriding the admin template. I personally like this option less because every time Django is updated I have to check that my changes aren't interfering with new features. However, sometimes this is the only way to do some more advanced things in the admin interface. I've done this in my own project, but like to keep the changes minimal.