1

I just started to learn Django and relatively new to Python.

I'm trying to understand Django's overview here

from django.db import models

class Reporter(models.Model):
    full_name = models.CharField(max_length=70)

    def __str__(self):              # __unicode__ on Python 2
        return self.full_name

class Article(models.Model):
    pub_date = models.DateField()
    headline = models.CharField(max_length=200)
    content = models.TextField()
    reporter = models.ForeignKey(Reporter)

    def __str__(self):              # __unicode__ on Python 2
        return self.headline

I wonder how could they do something like this:

# Create a new Reporter.
>>> r = Reporter(full_name='John Smith')

and

>>> a = Article(pub_date=date.today(), headline='Django is cool',
...     content='Yeah.', reporter=r)

Can somebody point out where are the constructors defined in the example classes above?

1 Answer 1

1

Python classes don't need explicit constructors. However, in this case they are constructed by the metaclass, django.db.models.base.BaseModel, which they inherit from models.Model.

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

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.