Posts

Showing posts from April, 2021

AppConfig Application Configuration - Configurable Attributes

  AppConfig: Application Configuration objects store metadata for an application. Some attributes can be configured in AppConfig subclasses.  Others are set by Django and read-only. AppConfig.name:  Full Python path to the application, ex. 'django.contrib.admin'. AppConfig.name attribute defines which application the configuration applies to.  It must be set in all AppConfig subclasses. It must be unique across a Django project. AppConfig.label:  Short name for the application, ex. 'admin' This attribute allows relabeling an application when two applications have conflicting labels.  It defaults to the last component of name.  It should be a valid Python identifier. It must be unique across a Django project. AppConfig.verbose_name: Human Readable name for the Application, ex. “Administration”. This attribute defaults to label.title(). AppConfig.path: File System Path to the application directory, ex. '/usr/lib/pythonX.Y/dist-packages/django/contrib/adm...

FILTERS_DISABLE_HELP_TEXT in Django Filters

FILTERS_DISABLE_HELP_TEXT:- Some filters provide informational help_text.  For example, csv-based filters (filters.BaseCSVFilter) inform users that “Multiple values may be    separated by commas”. You can set this to True to disable the help_text for all filters by removing the text from the rendered form’s output.

FILTERS_NULL_CHOICE_VALUE in Django Filters

FILTERS_NULL_CHOICE_VALUE:- Set the default value for ChoiceFilter.null_value . You may want to change this value if the default 'null' string conflicts with an actual choice. Default value is 'null'

FILTERS_NULL_CHOICE_LABEL in Dango Filters

FILTERS_NULL_CHOICE_LABEL:- Set the default value for ChoiceFilter.null_label . You may enable the null choice by setting a non-None value. Default value is None.

FILTERS_EMPTY_CHOICE_LABEL in Djangi Filters

FILTERS_EMPTY_CHOICE_LABEL:- Set the default value for ChoiceFilter.empty_label . You may disable the empty choice by setting this to None. Default value is '---------'

FILTERS_DEFAULT_LOOKUP_EXPR in Django Filters

FILTERS_DEFAULT_LOOKUP_EXPR:- Set the default lookup expression to be generated, when none is defined. Default value is 'exact'. 

ManyToManyField in Django Models

ManyToManyField is represented by django.forms.ModelMultipleChoiceField , which is a MultipleChoiceField whose choices are a model QuerySet .

ForeignKey in Django Models

ForeignKey is represented by django.forms.ModelChoiceField , which is a ChoiceField whose choices are a model QuerySet.

ChoiceFilter in Django

ChoiceFilter filter matches values in its choices argument. The choices must be explicitly passed when the filter is declared on the FilterSet. Example: class User(models.Model): STATUS_CHOICES = ( (0, 'Active'), (1, 'Inactive'), ) username = models.CharField(max_length=300) firstname = SubCharField(max_length=150) lastname = SubSubCharField(max_length=150) user_status = models.IntegerField(choices=STATUS_CHOICES, default=0)      class User_Filter(FilterSet):           user_status = ChoiceFilter(choices=STATUS_CHOICES)      class Meta:           model = User           fields = ['user_status'] ChoiceFilter also has arguments that enable a choice for not filtering, as well as a choice for filtering by None values. empty_label : The display label to use for the select choice to not filter. The choice may be disabled by setting this argument to...

BooleanFilter in Django

BooleanFilter filter matches a boolean, either True or False, used with BooleanField and NullBooleanField by default. Example: boolean_field_filter = filters.BooleanFilter(field_name='boolean_field_filter', label='Boolean Field Filter')     

UUIDFilter in Django

  UUIDFilter filter matches UUID values, used with models. UUIDField by default.

CharFilter in Django

  CharFilter filter does simple character matches, used with CharField and TextField by default. ex: char_filter_field_name = CharFilter(field_name='char_filter_field_name', lookup_expr='icontains', label='Char Filter Field Name', widget=forms.TextInput(attrs={'class': 'col-md-12 mr-2','placeholder': 'Please Enter Char Filter Field Name', 'style': 'width: 263px'}))     

SlugField in Django

In General terms A Slug is a short label for something, containing only letters, numbers, underscores or hyphens. They are generally used in URLs. A SlugField in Django is like a CharField where you can specify max_length attribute. If max_length is not specified, Django will use a default length of 50. Implies setting Field.db_index to True. It is often useful to automatically prepopulate a SlugField based on the value of some other value. You can do this automatically in the admin using prepopulated_fields. It uses validate_slug or validate_unicode_slug for validation. Syntax :      field_name = models.SlugField(max_length=100, **options)

verbose_name, verbose_name_plural in Django

verbose_name is a human-readable name for the field. If you are giving any verbose name then Django will automatically create it using the field’s attribute name, converting underscores to spaces.  The attribute in general changes the field name in admin interface.      Example:                field_name = models.Field(verbose_name = "give_your_readable_name_here") verbose_name_plural: The plural name for the object.         Examples:                     class Meta:                         verbose_name = "Bank"                         verbose_name_plural = "Banks"         Note:  If you are not giving verbose_name_plural then Django will use verbose_name + "s".

RedirectCycleError Exception in Django

  RedirectCycleError is raised when the test client detects a loop or an overly long chain of redirects.

Transaction Exceptions in Django

Transaction exceptions are defined in django.db.transaction. TransactionManagementError is raised for any and all problems related to database transactions.

Sessions Exceptions in Django

Sessions Exceptions are defined in django.contrib.sessions.exceptions. SessionInterrupted  is raised when a session is destroyed in a concurrent request.  It is subclass of BadRequest. It is new in Django 3.2.

Http Exceptions in Django

Http exceptions can be imported from django.http. UnreadablePostError  is raised when a user cancels an upload.

URL Resolver Exceptions in Django

URL Resolver Exceptions are defined in django.urls. Below are the URL Resolver Exceptions: Resolver404 NoReverseMatch                                        

Resolver404 Exception in Django

The Resolver404 exception is raised by resolve() if the path passed to resolve() doesn’t map to a view.  It’s a subclass of django.http.Http404.

NoReverseMatch Exception in Django

The NoReverseMatch exception is raised by django.urls when a matching URL in your URLconf cannot be identified based on the parameters supplied.

RegexValidator for mobile number validation in Python

RegexValidator:- A RegexValidator searches the provided value for a given regular expression with re.search(). By default, raises a ValidationError with message and code if a match is not found. Its behavior can be inverted by setting inverse_match to True, in which case the ValidationError is raised when a match is found. class RegexValidator(regex=None                                         , message=None                                         , code=None                                         , inverse_match=None                             ...

Remove White spaces in Python using Strip() function

Python String is immutable, which means we can’t change its value. Any function that manipulates string value returns a new string and we have to explicitly assign it to the string. Otherwise, the string value cannot change. Python String strip() function will remove leading and trailing whitespaces.          Ex: string1 = " Hello World.  Good Morning "               >> string1.strip()                     "Hello World.  Good Morning"