0

I have a Symbol model:

class Symbol(models.Model):

    market = models.CharField(         
        max_length=20,
        null=True,
        blank=True,
    )

and generate some Symbol model data.

What I was surprised at:

>>> s = Symbol.objects.first()
>>> s.i_dont_know = 1
>>> s.save()

It doesn't occur any error?

Why does it not occur any error?

11
  • Why do you think an error should occur? Commented Apr 30, 2018 at 7:10
  • 1
    Django would know which are defined fields and save only in the DB. There are other number of properties defined on the class/object of the model. Commented Apr 30, 2018 at 7:13
  • 1
    I'm not sure if you have misunderstood the object-oriented programming in Python. This is a general Python question and has nothing in particular with Django. You can add as many attributes as you wish to an object. As @Rohan wrote, Django saves only defined fields in the DB. Commented Apr 30, 2018 at 7:15
  • 1
    @BugHunter They are not fetching the first incorrectly. Django queryset managers have first() and last() methods which gives you the first and last objects in the related field. Commented Apr 30, 2018 at 7:22
  • 1
    @BugHunter: but you can use .first() and .none() as well, since it is the relation is a queryset. Commented Apr 30, 2018 at 8:48

2 Answers 2

2

When you call save method on model object then it will only update the model fields which we defined in the model. All other attributes of the model object will be ignored. That's the reason you didn't get any errors.

Sign up to request clarification or add additional context in comments.

Comments

0

Why does it not occur any error?

On a Model, you can define zero or more fields that have an equivalent in the database, but besides that, you can add zero or more functions, properties, attributes, etc. on the model as well.

You probably already did that somehow. For example:

class Symbol(models.Model):

    market = models.CharField(         
        max_length=20,
        null=True,
        blank=True,
    )

    def __init__(self, *args, **kwargs):
        super(Symbol, self).__init__(*args, **kwargs)
        self.cache = {}

    @property
    def market3(self):
        return self.market and self.market[:3]

After all those models are in fact Python objects, with some magic attached such that if you change an attribute and there is a field with the same name, then it corresponds to that field. But usually models also have some extra logic, and attributes to make them more intelligent, handle parts of a request at the model level, calculate properties that depend one one or more fields, etc.

By writing:

s.i_dont_know = 1

You attached an attribute to the s object with name 'i_dont_know' and value 1. Note however that this attribute is not persistent: if you save the object, and later reload it from the database, then all the extra data you attached to it, will be gone. Unless of course you define a setter that adds some extra logic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.