Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSahar Nasiri
7,454 PointsMeta Class
We do not build any object of Meta class what is it for? Where do we want to use this class?
4 Answers
Ben Godwin
6,395 PointsThe statement
class Meta:
isn't actually defining a new class. It's defining attributes of the class you're writing (in this case, where it should look for its database.)
Dariush Azimi
310 PointsHi Sahar
I'm not sure if your question is related to django and python but I explain it with respect to django.
In django models we use a Meta class to provide additional info about the model that we are working with. Keep in mind that this is not the same as Python's metaclass.
Lets look at an example:
Class Book(models.Model):
name = models.CharField(max_length=100)
author = models.CharField(max_length=100)
class Meta:
ordering = ["author"]
verbose_name_plural = "authors"
Here basically when a query is returned will be ordered by the author. In the next statement, we are stating what the plural form of author should be.
There are many other meta options available where you can find them in django docs. https://docs.djangoproject.com/en/dev/topics/db/models/
According to the docs "Model metadata is “anything that’s not a field”, such as ordering options (ordering), database table name (db_table), or human-readable singular and plural names (verbose_name and verbose_name_plural). None are required, and adding class Meta to a model is completely optional."
Hope it helps.
Sergio Niño
Full Stack JavaScript Techdegree Student 22,976 PointsHello, Sahar Nasiri what is it for? it's tell the Model what Database is belongs to We do that, by defining a class called Meta (this is just a class that called meta) inside of our Entry class in this case, (you can have classes inside of classes.) and it have one attribute which is 'database', because we're going to set database as being equal to 'db' which is our SQLite database we created up on line above.
I hope this helps clear it up. :)
Sahar Nasiri
7,454 PointsHi,
Thank you for the comment, my question was for long time age. But it just pops in my head that if we didn't make any object from a class why do we need to create it in a first place? I mean we just built a class named Meta and we never used this class.
Steven Parker
231,269 PointsYou might want to review the Modeling video from the previous section. At around 3:50 the use of the Meta sub-class is discussed, and how it relates to Model and describes attributes of the class it belongs to. It's not used to instantiate objects.
Sahar Nasiri
7,454 PointsI don't know how I got convinced before :)), although I watched the video you just mentioned many times, I still do not know why did he write that Meta Class? Nothing happens when I erase it and I don't understand the use of this class when we do not make any instances of it.
Sahar Nasiri
7,454 PointsSahar Nasiri
7,454 PointsThanks.
It makes sense, but this attribute is a class, right? Don't we have to make an instance of it in order to use it? I mean when we define an attribute in a class we have to use it somewhere, or we have to give it a value. We don not use this attribute anywhere, or give it any value, either.
Ben Godwin
6,395 PointsBen Godwin
6,395 PointsYou're right to point out the inconsistency. The more you learn about Python though the more crazy odd stuff you find that you 'just have to remember'...