<Django> setattr() & getattr(), Null & Blank, Field Validations, query_set.exists()

JunePyo Suh·2020년 5월 11일
0

setattr() and getattr()

According to the documentation, getattr(x, 'foobar') is equivalent to x.foobar, and setattr(x, 'foobar', 123) is equivalent to x.foobar = 123.

It is generally recommended to use setattr() and getattr() when getting or setting model field values from other classes for two reasons:
1. These methods come in handy when the variables are subject to change.

var1 = 'foo'
getattr(x, var1)

Moreover, getattr(x, 'foo', 42) takes in an optional default value, which is returned if the attribute is missing.

  1. If some field values are set as private, these methods are the only way to access the values from other classes.

Blank or Null?

Null is database-related. It defines if a given database column will accept null values or not . On the other hand, Blank is validation-related, meaning that it will be used during forms validation, such as when calling form.is_valid()

Having a field with null=True and blank=False will therefore imply that, on the database level, the field can be NULL, and on the application level, it is a required field.

However, it is important to avoid defining null=True for string-based fields such as CharField and TextField. Otherwise, there can be two possible values for "no data," which is None, and an empty string. This is redundant, and the Django convention is to use the empty string, not NULL.

Hence, for a string-based model field to be "nullable," prefer doing it this way:

text = models.TextField(max_length=500, blank=True)

The default values of null and blank are False.

If you need to accept NULL values for a BooleanField, use NullBooleanField instead.

unique=True

This sets the field to be unique.

field_name = models.Field(unique=True)

More Built-in Field Validations

Null

If True, Django will store empty values as NULL in the database. Default is False.

Blank

If True, the field is allowed to be blank. Default is False.

db_column

The name of the database column to use for this field. If this isn’t given, Django will use the field’s name.

Default

The default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created.

help_text

Extra “help” text to be displayed with the form widget. It’s useful for documentation even if your field isn’t used on a form.

primary_key

If True, this field is the primary key for the model.

editable

If False, the field will not be displayed in the admin or any other ModelForm. They are also skipped during model validation. Default is True.

error_messages

The error_messages argument lets you override the default messages that the field will raise. Pass in a dictionary with keys matching the error messages you want to override.

help_text

Extra “help” text to be displayed with the form widget. It’s useful for documentation even if your field isn’t used on a form.

verbose_name

A human-readable name for the field. If the verbose name isn’t given, Django will automatically create it using the field’s attribute name, converting underscores to spaces.

validators

A list of validators to run for this field. See the validators documentation for more information.

Unique

If True, this field must be unique throughout the table.

exists()

Useful for searches relating to both object membership in a QuerySet and to the existence of any objects in a QuerySet.

The most efficient method of finding whether a model with a unique field, such as a primary_key, is a member of a QuerySet is:

account = Account.objects.get(pk=123)
if some_queryset.filter(pk=account.pk).exists():
	print("Account contained in queryset")

0개의 댓글