How to have 2 different admin sites in a Django project?

You can subclass Django’s AdminSite (put it eg. in admin.py in your project root): from django.contrib.admin.sites import AdminSite class MyAdminSite(AdminSite): pass #or overwrite some methods for different functionality myadmin = MyAdminSite(name=”myadmin”) At least from 1.9 on you need to add the name parameter to make it work properly. This is used to create the revers … Read more

Django: access the parent instance from the Inline model admin

Django < 2.0 Answer: Use Django’s Request object (which you have access to) to retrieve the request.path_info, then retrieve the PK from the args in the resolve match. Example: from django.contrib import admin from django.core.urlresolvers import resolve from app.models import YourParentModel, YourInlineModel class YourInlineModelInline(admin.StackedInline): model = YourInlineModel def get_parent_object_from_request(self, request): “”” Returns the parent object … Read more

Dynamic fields in Django Admin

Here is a solution to the problem. Thanks to koniiiik i tried to solve this by extending the *get_fieldsets* method class ProductAdmin(admin.ModelAdmin): def get_fieldsets(self, request, obj=None): fieldsets = super(ProductAdmin, self).get_fieldsets(request, obj) fieldsets[0][1][‘fields’] += [‘foo’] return fieldsets If you use multiple fieldsets be sure to add the to the right fieldset by using the appropriate index.

django QueryDict only returns the last value of a list

From here This is a feature, not a bug. If you want a list of values for a key, use the following: values = request.POST.getlist(‘key’) And this should help retrieving list items from request.POST in django/python The function below converts a QueryDict object to a python dictionary. It’s a slight modification of Django’s QueryDict.dict() method. … Read more

How to limit file types on file uploads for ModelForms with FileFields?

Create a validation method like: def validate_file_extension(value): if not value.name.endswith(‘.pdf’): raise ValidationError(u’Error message’) and include it on the FileField validators like this: actual_file = models.FileField(upload_to=’uploaded_files’, validators=[validate_file_extension]) Also, instead of manually setting which extensions your model allows, you should create a list on your setting.py and iterate over it. Edit To filter for multiple files: def … Read more

Extending Django Admin Templates – altering change list

To expand on Yuji’s answer, here are some specifics on overriding change_list_results.html … Override changelist_view as described above in step 1, and also described here at djangoproject. Or auto-override by placing in the appropriate directory as in step 2 above. (Note that the step 2 path shown above is model-specific. App-specific would be /admin/<MyAppName>/change_list.html under … Read more

ModelForm with OneToOneField in Django

You have to create second form for PrinterAddress and handle both forms in you view: if all((profile_form.is_valid(), address_form.is_valid())): profile = profile_form.save() address = address_form.save(commit=False) address.printer_profile = profile address.save() Of course in the template you need to show both forms under one <form> tag 🙂 <form action=”” method=”post”> {% csrf_token %} {{ profile_form }} {{ address_form … Read more