Skip to content

Address Serializer

Fields

Field Description Required
id Auto-generated address ID. Read-only
user ForeignKey to the User model, linking the address to a specific user. Yes
name Name of the person associated with the address. Yes
mobile Mobile phone number of the person. Yes
address_line_1 First line of the address (e.g., street name, number). Yes
address_line_2 Second line of the address (optional, e.g., apartment number). No
landmark Landmark for easy identification of the address (optional). No
pincode Postal code for the address. Yes
country ForeignKey to the Country model. Indicates the country of the address. No
state_selection ForeignKey to the State model. Allows selection of a predefined state. No
state_custom Custom text field for specifying a state if it is not available in the list. No
town_city Name of the town or city. Yes
is_default Boolean flag to indicate if this is the default address for the user. No
created_at Timestamp of when the address was created. Read-only
updated_at Timestamp of when the address was last updated. Read-only

Relationships

  • user: This field establishes a one-to-many relationship with the User model, as one user can have multiple addresses.
  • country: This foreign key links to a Country model, which stores country details.
  • state_selection: This foreign key links to the State model, which contains a predefined list of states.

Methods

  • __str__(self): Returns a string representation of the address in the format {name} - {address_line_1}.

Validation

  • The state_selection and state_custom fields are mutually exclusive. You can either select a state from the predefined list (state_selection) or specify a custom state (state_custom), but not both at the same time.
  • If is_default is set to True, all other addresses associated with the user will have is_default set to False.

Examples

Creating a new Address:

{
    user=user,
    name="John Doe",
    mobile="+1234567890",
    address_line_1="123 Main Street",
    address_line_2="Apt 101",
    landmark="Near Park",
    pincode="123456",
    country=country,
    state_selection=state,
    state_custom="",  # If you choose a state_selection, this should be empty
    town_city="New York",
    is_default=True
}