How do I retrieve a Django model class dynamically?

I think you’re looking for this: from django.db.models.loading import get_model model = get_model(‘app_name’, ‘model_name’) There are other methods, of course, but this is the way I’d handle it if you don’t know what models file you need to import into your namespace. (Note there’s really no way to safely get a model without first knowing … Read more

How to display uploaded images in “Change List” page in Django Admin?

You can create a model instance method with another name, allow HTML tags for its output and add this method as a list field. Here is an example: First add a new method returning the HTML for the image inclusion: class Article(models.Model): … def admin_image(self): return ‘<img src=”%s”/>’ % self.img admin_image.allow_tags = True Then add … Read more

Django formsets: make first required?

Found a better solution: class RequiredFormSet(BaseFormSet): def __init__(self, *args, **kwargs): super(RequiredFormSet, self).__init__(*args, **kwargs) for form in self.forms: form.empty_permitted = False Then create your formset like this: MyFormSet = formset_factory(MyForm, formset=RequiredFormSet) I really don’t know why this wasn’t an option to begin with… but, whatever. It only took a few hours of my life to figure … Read more

django – get user logged into test client

The test client is request-agnostic. It doesn’t inherently hold information about what users are logged in. (Neither does your actual webserver or the Django dev server, either, for obvious reasons, and those same reasons apply here). login is simply a convenience method on the test client to essentially mimic a POST to /login/ with the … Read more

Change Django ModelChoiceField to show users’ full names rather than usernames

You can setup a custom ModelChoiceField that will return whatever label you’d like. Place something like this within a or wherever applicable. class UserModelChoiceField(ModelChoiceField): def label_from_instance(self, obj): return obj.get_full_name() Then when creating your form, simply use that field UserModelChoiceField(queryset=User.objects.filter(is_staff=False), required = False) More info can be found here

Django: Admin: changing the widget of the field in Admin

UPDATE 1: Code that gets me done with 1) (don’t forget tot pass CHOICES to the BooleanField in the model) from main.models import TagCat from django.contrib import admin from django import forms class MyTagCatAdminForm(forms.ModelForm): class Meta: model = TagCat widgets = { ‘by_admin’: forms.RadioSelect } fields=”__all__” # required for Django 3.x class TagCatAdmin(admin.ModelAdmin): form = … Read more

Sanitizing HTML in submitted form data

strip_tags actually removes the tags from the input, which may not be what you want. To convert a string to a “safe string” with angle brackets, ampersands and quotes converted to the corresponding HTML entities, you can use the escape filter: from django.utils.html import escape message = escape(form.cleaned_data[‘message’])