21π
β
This is probably coming in late. But for other viewers reference,
def get_form(self, request, obj=None, **kwargs):
form = super(ProductAdmin, self).get_form(request, obj, **kwargs)
form.base_fields['category'].widget.can_add_related = False
return form
π€Mo J. Mughrabi
3π
can_add_related
seems to be an attribute on the widget, not the field, so try:
self.fields['person'].widget.can_add_related = False
π€Timmy O'Mahony
- Django β using multiple foreign key to the same model
- Where do I modify the admin view code for a 3rd party app?
1π
Alternative approach, with changing widget options *before* the form is instantiated:
class MyAdmin(django.contrib.admin.ModelAdmin):
def formfield_for_dbfield(self, *args, **kwargs):
formfield = super().formfield_for_dbfield(*args, **kwargs)
if hasattr(formfield, "widget"):
formfield.widget.can_add_related = False
formfield.widget.can_delete_related = False
formfield.widget.can_change_related = False
else:
pass # this relation doesn't have an admin page to add/delete/change
return formfield
π€Art
- I have a OneToOne relationship between two objects of the same class in a Django app. Is it possible to enforce the uniqueness of this relationship?
- Why does python new york time zone display 4:56 instead 4:00?
- Django, register user with first name and last name?
1π
Another approach, if you are defining an Inline model and use it in your admin, would be to overwrite the get_formset method:
from django.contrib import admin
class MyModelInline(admin.TabularInline):
model = MyModel
extra = 0
min_num = 1
max_num = 10
fields = [
'some_field'
]
def get_formset(self, request, obj=None, **kwargs):
fs = super().get_formset(request, obj, **kwargs)
fs.form.base_fields['some_field'].widget.can_add_related = False
fs.form.base_fields['some_field'].widget.can_change_related = False
fs.form.base_fields['some_field'].widget.can_delete_related = False
return fs
π€babis21
- Adding SSL certificates to a mysql docker container
- Django makemigrations AttributeError: 'str' object has no attribute '_meta'
- Django Testing β Hard code URLs or Not
- Writable nested serializer in django-rest-framework?
0π
For TabularInline you can combine the answer from @Art and @babis21:
We can use the @Art answer here as well using the following:
def get_formset(self, request, obj=None, **kwargs):
res = super().get_formset(request, obj=None, **kwargs)
for formfield in res.form.base_fields.values():
if hasattr(formfield, "widget"):
formfield.widget.can_add_related = False
formfield.widget.can_delete_related = False
formfield.widget.can_change_related = False
return res
π€kholioeg
- Python multiple inheritance function overriding and ListView in django
- Django form field grouping
- Runtime error:App registry isn't ready yet
- Django-registration, force unique e-mail
0π
I coded this mixin:
class AdminModelNoAddOrChangeForeignKeyMixin:
# For admin.ModelAdmin
def get_form(self, request, obj=None, **kwargs):
form = super().get_form(request, obj, **kwargs)
for field_name, field in form.base_fields.items():
if isinstance(self.model._meta.get_field(field_name), ForeignKey):
field.widget.can_add_related = False
field.widget.can_change_related = False
return form
# For admin.TabularInline and admin.StackedInline
def get_formset(self, request, obj=None, **kwargs):
fs = super().get_formset(request, obj, **kwargs)
for field_name, field in fs.form.base_fields.items():
if isinstance(self.model._meta.get_field(field_name), ForeignKey):
field.widget.can_add_related = False
field.widget.can_change_related = False
return fs
Works for all foreign key fields as mixin for Inline and ModelAdmin:
class MyInline(AdminModelNoAddOrChangeForeignKeyMixin, admin.TabularInline):
whatever
class DigitalDistributionDataAdmin(AdminModelNoAddOrChangeForeignKeyMixin, admin.ModelAdmin):
whatever
π€user1383029
- Werkzeug server is shutting down in Django application
- Django: I get a [relation "auth_group" does not exist] error after syncdb
- How to test django application placed in subfolder?
- Cannot open manage.py after installing django
- Python sqlite use in terminal -django
Source:stackexchange.com