In this link (for the ReadOnlyField): http://www.django-rest-framework.org/api-guide/fields/#readonlyfield it says "This field is used by default with ModelSerializer when including field names that relate to an attribute rather than a model field". With that said, can you give me an example of a Model field name that is an "attribute" and a Model field name that is a "field"?
In Django
, a model field
pertains to a column in the database. On the other hand, a model attribute
pertains to a method or property that is added to a model
.
class MyModel(models.Model):
name = models.CharField()
quantity = models.IntegerField()
price = models.DecimalField()
@property
def description(self):
return '{}x of {}'.format(quantity, name)
def compute_total(self):
return quantity * price
In the example above, name
, quantity
and price
are model fields
since they are columns in the database. Meanwhile, description
and compute_total
are model attributes
.
If 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