django-admin

How to show a message to a django admin after saving a model?

半腔热情 提交于 2020-06-24 08:07:52
问题 I want to display a message to admins after they save a particular model, something like "Now enable the series". I can see how I'd do this if it were a list action (message_user) but I can't see how to do this from the main CRUD form. Does anyone know how? Thanks 回答1: Old question, but worth at least a small example as I think this is quite a common issue. @Davor Lucic pointed to the right solution. As of today, Django ships with a cool message framework that helps a lot with this. So, say

Django ModelAdmin.get_urls() not registering custom urls

筅森魡賤 提交于 2020-06-23 05:13:02
问题 I am trying to create custom views for my models in Django admin site. I created ModelAdmin for my model named Document like this: from django.http import HttpResponse from django.urls import path from django.contrib import admin from my_app.models import Document @admin.register(Document) class DocumentAdmin(admin.ModelAdmin): def get_urls(self): urls = super().get_urls() custom_urls = [ path('my-view/', self.admin_site.admin_view(self.my_view)) ] return urls + custom_urls def my_view(self,

Django ModelAdmin.get_urls() not registering custom urls

独自空忆成欢 提交于 2020-06-23 05:12:22
问题 I am trying to create custom views for my models in Django admin site. I created ModelAdmin for my model named Document like this: from django.http import HttpResponse from django.urls import path from django.contrib import admin from my_app.models import Document @admin.register(Document) class DocumentAdmin(admin.ModelAdmin): def get_urls(self): urls = super().get_urls() custom_urls = [ path('my-view/', self.admin_site.admin_view(self.my_view)) ] return urls + custom_urls def my_view(self,

How to show related object popup when the user clicks on selected autocomplete field options in Django admin?

青春壹個敷衍的年華 提交于 2020-06-17 09:43:26
问题 I want to show the standard related object popup when the user clicks on a selected option in a Django admin autocomplete multi-select field, like it works when clicking the ForeignKey field pencil icon 🖉. The models are as follows: class Author(models.Model): name = models.CharField(_('name'), max_length=160) class Book(models.Model): authors = models.ManyToManyField(Author, verbose_name=_('authors'), blank=True) ... Is it possible to do this by extending Django admin? 回答1: I found that

Adding extended Profile model into custom user models admin

倖福魔咒の 提交于 2020-06-16 04:55:50
问题 How can i add extended Profile model fields (fields which are not available in custom user model fields) into custom users admin users.admin ? what i am trying to do is that i want too see Profile model fields like photo, date_of_birth, country, phone etc.. inside the Personal Info(see in image) & i can make changes in it from here. profile model from django.db import models from django.dispatch import receiver from django.urls import reverse from django.db.models.signals import post_save

Django Admin: JSONField default empty dict wont save in admin

时光怂恿深爱的人放手 提交于 2020-06-10 09:19:30
问题 in my model defn i have from django.contrib.postgres.fields import JSONField ..... ....... ......... media_data=JSONField(default=dict) I created a default admin When i attempt to save without touching the field, i get a this field is required error. It looks like a form validation issue because I can programatically save the model instance from code without issue. Why is this happening? have i missed something silly? 回答1: What happening. when dive into the source code. we can see the

Select item in Django admin inline with radio buttons

谁说胖子不能爱 提交于 2020-05-25 05:45:28
问题 Here's part of my models.py: class Person(models.Model): birth_year = WideYear(null=True, blank=True) birth_year_uncertain = models.BooleanField() death_year = WideYear(null=True, blank=True) death_year_uncertain = models.BooleanField() flourit_year = WideYear(null=True, blank=True) flourit_year_uncertain = models.BooleanField() FLOURIT_CHOICES = ( (u'D', u'Birth and death dates'), (u'F', u'Flourit date'), ) use_flourit = models.CharField('Date(s) to use', max_length=2, choices=FLOURIT

How to show all fields of model in admin page?

落爺英雄遲暮 提交于 2020-05-24 08:44:48
问题 here is the models page In this picture, only the title shows up on here, I used: def __unicode__(self): return self.title; here is the each individual objects How do I show all these fields? How do I show all the fields in each Model page? 回答1: By default, the admin layout only shows what is returned from the object's unicode function. To display something else you need to create a custom admin form in app_dir/admin.py . See here: https://docs.djangoproject.com/en/dev/ref/contrib/admin/

Update many-to-many field in django with field name in variable

人走茶凉 提交于 2020-05-18 05:28:08
问题 I want to be able to update a many-to-many field on a model, when the name of the field is stored in a variable. This is relatively easy for a normal field (with kwargs in the constructor), or even a foreign key, but not so for a many-to-many field. Here's the situation: class Book(models.Model): title = models.CharField(max_length=30) authors = models.ManyToManyField(Author) field_name = 'title' new = Book(**{field_name:'My Book'}) # This sets title to mybook new.save() my_authors = Author

How to filter ModelAdmin autocomplete_fields results with the context of limit_choices_to

佐手、 提交于 2020-05-15 08:04:34
问题 I have a situation where I wish to utilize Django's autocomplete admin widget, that respects a referencing models field limitation. For example I have the following Collection model that has the attribute kind with specified choices. class Collection(models.Model): ... COLLECTION_KINDS = ( ('personal', 'Personal'), ('collaborative', 'Collaborative'), ) name = models.CharField() kind = models.CharField(choices=COLLECTION_KINDS) ... Another model ScheduledCollection references Collection with a