Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django models: Good way of storing a multiline line address as one model field

I am building some models, and I need to collect an address. Thing is, I want the address to be be collected in one model field, yet the fields for the address could span over multiple lines.

For example:

street:

city:

zip:

state:

Mine don't look like these, but you get the idea. The data needs to be stored in one model field.

like image 848
ApathyBear Avatar asked May 08 '14 15:05

ApathyBear


People also ask

What is proxy model in Django?

Proxy models allow us to change the Python behavior of a model without changing the database. vehicles/models.py. from django.db import models. class Car(models.Model): vin = models.CharField(max_length=17)

What is PK in Django model?

pk is short for primary key, which is a unique identifier for each record in a database. Every Django model has a field which serves as its primary key, and whatever other name it has, it can also be referred to as "pk".

WHAT IS models ForeignKey?

ForeignKey is a Django ORM field-to-column mapping for creating and working with relationships between tables in relational databases. ForeignKey is defined within the django. db.

How do you exclude a specific field from a ModelForm?

Set the exclude attribute of the ModelForm 's inner Meta class to a list of fields to be excluded from the form.


1 Answers

TextField is a perfect choice here:

class TextField([**options])

A large text field. The default form widget for this field is a Textarea.

Since textarea is used as a widget, it "handles" newlines for you.


I would recommend rethink the idea of storing the address inside the model field. Instead, consider having a special model(s) handling the address, since, in the future, you would probably want to query the data by city, country, street, zip etc. It would more clean, transparent and easy to filter.

See also:

  • how to model a postal address
  • django-postal
  • django-address
  • Address model snippet 1
  • Address model snippet 2
  • django.contrib.gis Address model example
like image 83
alecxe Avatar answered Oct 29 '22 16:10

alecxe