Displaying DynamoDB data in Django admin panel - django

I want to show data from DynamoDB on the Django admin panel page. I have tried PynamoDB to create the model and register it with the admin panel.
I am facing the following two issues:
Getting only attribute 'id' instead of all attributes from the DynamoDB table. I have confirmed this issue while fetching attribute_definitions from the DynamoDB table using boto3.
attribute_definitions = table_name.attribute_definitions
The second issue I am facing is that I get errors while registering the created Model(by using PynamoDB) with the admin panel. I get the issue
"TypeError: 'MetaModel' object is not iterable"

you can pass this data to admin template using this method
changelist_view
Ex:
class DynmoDbAdmin(admin.ModelAdmin):
def changelist_view(self, request, extra_context=None):
get data from dynamodb
add this data to extra_context
return super().changelist_view(request, extra_context=extra_context)
after that
you need to extend admin template
follow this pattern
add 'APP_DIRS': True, to your settings file.
go to your app and create this folders templates/admin/model_name
in this folder create file change_list.html
this links will help you.
https://docs.djangoproject.com/en/3.2/howto/overriding-templates/
How to override and extend basic Django admin templates?

Related

How do I create a dynamic "About Me" page in Django?

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.

Is it possible to allow to create objects from the list_display view of the django admin site?

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.

Add a custom column in django database

Iam little new to Django and Iam struggling to add column in Django Database.
I was working to create a subcategory for products, but the I want those subcategories to be predefined. That is for example "Fashion" category could have subcategory of 'Men', 'Women', 'Kids'.
I want a table in DB of these subcategories, without having the user or admin panel option to manipulate these field. User can only select article belong to which subcategory.
I go through a few documentation, but could understood much:
1. https://docs.djangoproject.com/en/3.0/howto/custom-model-fields/
2. Adding fields to an already existing database for Django (version < 1.7)
Please suggest me how to add these predefined table values.
Create subcategories model and model admin then in model admin you can disable change permission like this
Subcategories admin model
def has_change_permission(self, request, obj=None):
return False
Now you can add,delete and view only permission. User and admin can't change them.

How can I update a model on the Django admin page when the model is click to be viewed?

I have a function that updates my Orders model. In my admin.py, I register the model, and have a admin.ModelAdmin class associated with it. I want to call a function that runs when the registered model is clicked on the admin page, so that it updates before the users sees any information about the orders. I've tried just adding the function:
class OrdersAdmin(admin.ModelAdmin):
...
update_orders()
But this just results in it being updated when I run the server, and not when the "Orders" model is clicked in the admin. I've looked into admin actions, but that is only related to making changes to various fields of the model after the page is populated with objects from Orders Model.
How can I pass a function to be called when I click on the model in the admin page? Thanks.
I guess the best way to do this is to override the get_absolute_url method in your model See docs.
def get_absolute_url(self):
#update you orders here .....
from django.urls import reverse
return reverse('admin:yourapp_yourmodel_change', args=[str(self.id)])

Django admin - Keep updating a model and do not create more than one

Im not sure how to put this title.
But what I need is a simple model that I can just update everytime. Lets say I have a Settings model ,and these settings I just want to update or change every now and again. So there is no need to add another object to that model.
Is there suck a field or type of admin-model that I can use?
I could otherwise just keep updating the same object, but I do not want the user to be able to just "Add Setting".
in admin you can specify various permissions. to remove the "add" functionality:
class MyModelAdmin(admin.ModelAdmin):
def has_add_permission(self, request): return False
make sure you create your first and only settings object when you deploy the application.
here is a discussion about singleton models in django: How about having a SingletonModel in Django?