django-modeladmin

Django admin list page takes forever to load after overriding get_queryset method

巧了我就是萌 提交于 2020-07-03 13:15:08
问题 I have this model admin - class NewsAdmin(ImageWidgetAdmin): image_fields = ['featured_image'] list_per_page = 20 list_display = ('heading', 'category', 'status', 'is_active', 'created_at', 'published_at', 'created_by', 'published_by') list_editable = ('category', 'status', 'is_active') list_filter = ('published_at', 'created_at', 'status', 'is_active', 'created_by', 'published_by',) search_fields = ('heading', 'category', 'tags', 'source') actions = [enable_object, disable_object, status

Django ModelAdmin with request/user based restrictions on the fieldsets (needs to be Thread Safe)

拟墨画扇 提交于 2020-01-25 06:28:04
问题 I have several very customized Django ModelAdmins that I would like to add extra field(s) if the user is a superuser. I found somewhere that someone said to override the get_fieldsets method like this def get_fieldsets(self, request, obj=None): fieldsets = super(PageAdmin, self).get_fieldsets(request, obj) if request.user.is_superuser: fieldsets[0][1]['fields'].insert(0,'group') fieldsets[0][1]['fields'].insert(2,'is_live') else: groups = request.user.page_groups.filter( is_live = True, ) if

Django - ForeignKey field initial value definition in Admin

◇◆丶佛笑我妖孽 提交于 2020-01-02 06:16:48
问题 I have a Person model, which has a ForeignKey field to itself, called mother . When the user goes to the 'add' admin form, I want to define an initial value for mother , in case there is a GET('mother') parameter, or leave it blank, in case there is not. I have actually 2 questions: How to access request inside ModelAdmin ? How to define initial value for a ForeignKey field? In models.py: class Person(models.Model): name=models.CharField() mother=models.ForeignKey('self') In admin.py: class

Django - Get object id in render_change_form (ModelAdmin)

只谈情不闲聊 提交于 2019-12-21 23:29:37
问题 I have these two models and modeladmin. When adding a new host in the list of available hostuser only appear hostusers that are not assigned to another host. The issue is if I edit an already created host its actual hostuser id is also filtered so I want to do is to exclude hostuser id that is currently assigned. How I can specify in the exclude the current id from the hostuser field? The statement that I need is written between * Thanks in advance Models.py class HostUser(models.Model): name

Django's admin does not login after custom authentication

感情迁移 提交于 2019-12-14 03:34:11
问题 The custom authentication I wrote follows the instructions from the docs. I am able to register, login, and logout the user, no problem there. Then, when I create a superuser python manage.py createsuperuser , it creates a user in the database, but it does not let me login when I go to the admin page and try to login saying Please enter the correct email address and password for a staff account. Note that both fields may be case-sensitive. Here is my code: models.py: from __future__ import

Why does Django keeps asking content types are stale and need to be deleted

余生长醉 提交于 2019-12-13 12:29:58
问题 I've tried everything found: Can stale content types be automatically deleted in Django? Deleting unused Models, stale content types prompt InvalidBasesError: Cannot resolve bases for [<ModelState: 'users.GroupProxy'>] Django Wagtail CMS migrate: Cannot resolve bases for [<ModelState: 'app.CustomPage'> Django migrate with zinnia- InvalidBasesError: Cannot resolve bases for [<ModelState: 'zinnia.Author'>] So here's my problem: I have: a ComicBook that has a many-to-many Planche 's a Planche

How may I overrride the ModelAdmin so that it displays components of two models?

天大地大妈咪最大 提交于 2019-12-13 05:11:51
问题 I want to override the ModelAdmin . But I want to include the functionalities of two models into one. However I'm not considering merging the two models into one. Is there any other way in which I can override it and include the functionalities of both of my models into one ModelAdmin ? 回答1: It depends on a relation between your models. Take a look to InlineModelAdmin : https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.InlineModelAdmin 来源: https://stackoverflow.com

add help text to a read only field in django admin view

僤鯓⒐⒋嵵緔 提交于 2019-12-10 21:07:14
问题 Below is my admin view: @admin.register(AuditStashAwsMasterPolicies) class AuditPoliciesAdmin(reversion.VersionAdmin): exclude = ['source_policy_path', 'source_state', 'target_state'] readonly_fields = ['comparison_date', 'source', 'source_policy_name', 'target', 'target_policy_name', 'target_policy_path', 'policy_difference'] def policy_difference(self, instance): return drift.compare_two_policies(instance.source, instance.source_policy_name, instance.source_policy_path, instance.target,

How to add a line number to each row of a tabularinline block

不问归期 提交于 2019-12-08 06:40:26
问题 I have a ModelAdmin class with an inline of type TabularInline. What I would like is for each row of the TabularInline to have a line number displayed to the left of it. This number would increment as new records are added to the inline, and would be displayed when the form is being edited. I prefer the line number not be a part of the model for the inlined data, but rather be generated each time a new record is added to or displayed by the inline block. I don't need to keep this number in

Django - ForeignKey field initial value definition in Admin

≯℡__Kan透↙ 提交于 2019-12-05 14:17:06
I have a Person model, which has a ForeignKey field to itself, called mother . When the user goes to the 'add' admin form, I want to define an initial value for mother , in case there is a GET('mother') parameter, or leave it blank, in case there is not. I have actually 2 questions: How to access request inside ModelAdmin ? How to define initial value for a ForeignKey field? In models.py: class Person(models.Model): name=models.CharField() mother=models.ForeignKey('self') In admin.py: class PersonAdminForm(forms.ModelForm): class Meta: model = Person class PersonAdmin(admin.ModelAdmin): mother