Posts

Showing posts with the label Models

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.

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".

Django TimeField in Models

models.py -----------------------------------------------------------------------  time_field_name = models.TimeField(auto_now=False, auto_now_add=False, **options)