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.
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)
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".
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.
Set the exclude attribute of the ModelForm 's inner Meta class to a list of fields to be excluded from the form.
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:
django-postal
django-address
django.contrib.gis
Address model exampleIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With